{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4e1beb47-7f61-4294-ab05-d6b5543fad24",
   "metadata": {},
   "source": [
    "# 分类算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "67ff0501-36fa-4ce2-b65b-e2eccafbdbf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import classification_report   #分类\n",
    "from sklearn.metrics import confusion_matrix        #混淆矩阵\n",
    "from sklearn.metrics import roc_curve               #\n",
    "from sklearn.metrics import auc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "456b9c16-7ffa-4eb3-89f5-6afda6026cfc",
   "metadata": {},
   "source": [
    "# 目标任务：用LogisticRegression算法预测用户信用好坏"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5dbee121-4146-4be5-be22-51c35f012b5c",
   "metadata": {},
   "source": [
    "# 1、数据理解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "1facfcf4-ee44-4017-9acf-53516eae7910",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>checking</th>\n",
       "      <th>duration</th>\n",
       "      <th>history</th>\n",
       "      <th>purpose</th>\n",
       "      <th>amount</th>\n",
       "      <th>savings</th>\n",
       "      <th>employed</th>\n",
       "      <th>installp</th>\n",
       "      <th>marital</th>\n",
       "      <th>coapp</th>\n",
       "      <th>...</th>\n",
       "      <th>property</th>\n",
       "      <th>age</th>\n",
       "      <th>other</th>\n",
       "      <th>housing</th>\n",
       "      <th>existcr</th>\n",
       "      <th>job</th>\n",
       "      <th>depends</th>\n",
       "      <th>telephon</th>\n",
       "      <th>foreign</th>\n",
       "      <th>good_bad</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1169</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>67</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>good</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>48</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5951</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>22</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>bad</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4</td>\n",
       "      <td>12</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>2096</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>49</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>good</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>42</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>7882</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>45</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>good</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>24</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>4870</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>53</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>bad</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>4</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1736</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>31</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>good</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3857</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>40</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>good</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>4</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>804</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>38</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>good</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>1</td>\n",
       "      <td>45</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1845</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>bad</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>2</td>\n",
       "      <td>45</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>4576</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>27</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>good</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     checking  duration  history purpose  amount  savings  employed  installp  \\\n",
       "0           1         6        4       3    1169        5         5         4   \n",
       "1           2        48        2       3    5951        1         3         2   \n",
       "2           4        12        4       6    2096        1         4         2   \n",
       "3           1        42        2       2    7882        1         4         2   \n",
       "4           1        24        3       0    4870        1         3         3   \n",
       "..        ...       ...      ...     ...     ...      ...       ...       ...   \n",
       "995         4        12        2       2    1736        1         4         3   \n",
       "996         1        30        2       1    3857        1         3         4   \n",
       "997         4        12        2       3     804        1         5         4   \n",
       "998         1        45        2       3    1845        1         3         4   \n",
       "999         2        45        4       1    4576        2         1         3   \n",
       "\n",
       "     marital  coapp  ...  property  age  other  housing  existcr  job  \\\n",
       "0          3      1  ...         1   67      3        2        2    3   \n",
       "1          2      1  ...         1   22      3        2        1    3   \n",
       "2          3      1  ...         1   49      3        2        1    2   \n",
       "3          3      3  ...         2   45      3        3        1    3   \n",
       "4          3      1  ...         4   53      3        3        2    3   \n",
       "..       ...    ...  ...       ...  ...    ...      ...      ...  ...   \n",
       "995        2      1  ...         1   31      3        2        1    2   \n",
       "996        1      1  ...         2   40      3        2        1    4   \n",
       "997        3      1  ...         3   38      3        2        1    3   \n",
       "998        3      1  ...         4   23      3        3        1    3   \n",
       "999        3      1  ...         3   27      3        2        1    3   \n",
       "\n",
       "     depends  telephon  foreign  good_bad  \n",
       "0          1         2        1      good  \n",
       "1          1         1        1       bad  \n",
       "2          2         1        1      good  \n",
       "3          2         1        1      good  \n",
       "4          2         1        1       bad  \n",
       "..       ...       ...      ...       ...  \n",
       "995        1         1        1      good  \n",
       "996        1         2        1      good  \n",
       "997        1         1        1      good  \n",
       "998        1         2        1       bad  \n",
       "999        1         1        1      good  \n",
       "\n",
       "[1000 rows x 21 columns]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用pandas导入csv数据，查看前5行导入结果看是否正常\n",
    "import pandas as pd\n",
    "credit_df = pd.read_csv(\"D:/work/machine-learning/datasets/credit/credit.csv\")\n",
    "credit_df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb1f3ecc-b806-4b82-b8fb-a4bbb8145630",
   "metadata": {},
   "source": [
    "## 数据的检查一定要细致\n",
    "- info()\n",
    "- describe()\n",
    "- isnull()\n",
    "- sum()\n",
    "- duplicated()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c31575d3-5021-4a99-9437-9c498055ba1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1000 entries, 0 to 999\n",
      "Data columns (total 21 columns):\n",
      " #   Column    Non-Null Count  Dtype \n",
      "---  ------    --------------  ----- \n",
      " 0   checking  1000 non-null   int64 \n",
      " 1   duration  1000 non-null   int64 \n",
      " 2   history   1000 non-null   int64 \n",
      " 3   purpose   1000 non-null   object\n",
      " 4   amount    1000 non-null   int64 \n",
      " 5   savings   1000 non-null   int64 \n",
      " 6   employed  1000 non-null   int64 \n",
      " 7   installp  1000 non-null   int64 \n",
      " 8   marital   1000 non-null   int64 \n",
      " 9   coapp     1000 non-null   int64 \n",
      " 10  resident  1000 non-null   int64 \n",
      " 11  property  1000 non-null   int64 \n",
      " 12  age       1000 non-null   int64 \n",
      " 13  other     1000 non-null   int64 \n",
      " 14  housing   1000 non-null   int64 \n",
      " 15  existcr   1000 non-null   int64 \n",
      " 16  job       1000 non-null   int64 \n",
      " 17  depends   1000 non-null   int64 \n",
      " 18  telephon  1000 non-null   int64 \n",
      " 19  foreign   1000 non-null   int64 \n",
      " 20  good_bad  1000 non-null   object\n",
      "dtypes: int64(19), object(2)\n",
      "memory usage: 164.2+ KB\n"
     ]
    }
   ],
   "source": [
    "# 通过数据对象的info()函数查看数据对象的字段情况--记录数，缺失值数量，字段类型等\n",
    "credit_df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8195532b-d37c-419d-8763-820e3c1c1f1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>842302</th>\n",
       "      <th>17.99</th>\n",
       "      <th>10.38</th>\n",
       "      <th>122.8</th>\n",
       "      <th>1001</th>\n",
       "      <th>0.1184</th>\n",
       "      <th>0.2776</th>\n",
       "      <th>0.3001</th>\n",
       "      <th>0.1471</th>\n",
       "      <th>0.2419</th>\n",
       "      <th>...</th>\n",
       "      <th>25.38</th>\n",
       "      <th>17.33</th>\n",
       "      <th>184.6</th>\n",
       "      <th>2019</th>\n",
       "      <th>0.1622</th>\n",
       "      <th>0.6656</th>\n",
       "      <th>0.7119</th>\n",
       "      <th>0.2654</th>\n",
       "      <th>0.4601</th>\n",
       "      <th>0.1189</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>5.680000e+02</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>568.00000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>3.042382e+07</td>\n",
       "      <td>14.120491</td>\n",
       "      <td>19.305335</td>\n",
       "      <td>91.914754</td>\n",
       "      <td>654.279754</td>\n",
       "      <td>0.096321</td>\n",
       "      <td>0.104036</td>\n",
       "      <td>0.088427</td>\n",
       "      <td>0.048746</td>\n",
       "      <td>0.181055</td>\n",
       "      <td>...</td>\n",
       "      <td>16.25315</td>\n",
       "      <td>25.691919</td>\n",
       "      <td>107.125053</td>\n",
       "      <td>878.578873</td>\n",
       "      <td>0.132316</td>\n",
       "      <td>0.253541</td>\n",
       "      <td>0.271414</td>\n",
       "      <td>0.114341</td>\n",
       "      <td>0.289776</td>\n",
       "      <td>0.083884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>1.251246e+08</td>\n",
       "      <td>3.523416</td>\n",
       "      <td>4.288506</td>\n",
       "      <td>24.285848</td>\n",
       "      <td>351.923751</td>\n",
       "      <td>0.014046</td>\n",
       "      <td>0.052355</td>\n",
       "      <td>0.079294</td>\n",
       "      <td>0.038617</td>\n",
       "      <td>0.027319</td>\n",
       "      <td>...</td>\n",
       "      <td>4.82232</td>\n",
       "      <td>6.141662</td>\n",
       "      <td>33.474687</td>\n",
       "      <td>567.846267</td>\n",
       "      <td>0.022818</td>\n",
       "      <td>0.156523</td>\n",
       "      <td>0.207989</td>\n",
       "      <td>0.065484</td>\n",
       "      <td>0.061508</td>\n",
       "      <td>0.018017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>8.670000e+03</td>\n",
       "      <td>6.981000</td>\n",
       "      <td>9.710000</td>\n",
       "      <td>43.790000</td>\n",
       "      <td>143.500000</td>\n",
       "      <td>0.052630</td>\n",
       "      <td>0.019380</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.106000</td>\n",
       "      <td>...</td>\n",
       "      <td>7.93000</td>\n",
       "      <td>12.020000</td>\n",
       "      <td>50.410000</td>\n",
       "      <td>185.200000</td>\n",
       "      <td>0.071170</td>\n",
       "      <td>0.027290</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.156500</td>\n",
       "      <td>0.055040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>8.692225e+05</td>\n",
       "      <td>11.697500</td>\n",
       "      <td>16.177500</td>\n",
       "      <td>75.135000</td>\n",
       "      <td>420.175000</td>\n",
       "      <td>0.086290</td>\n",
       "      <td>0.064815</td>\n",
       "      <td>0.029540</td>\n",
       "      <td>0.020310</td>\n",
       "      <td>0.161900</td>\n",
       "      <td>...</td>\n",
       "      <td>13.01000</td>\n",
       "      <td>21.095000</td>\n",
       "      <td>84.102500</td>\n",
       "      <td>514.975000</td>\n",
       "      <td>0.116600</td>\n",
       "      <td>0.146900</td>\n",
       "      <td>0.114475</td>\n",
       "      <td>0.064730</td>\n",
       "      <td>0.250350</td>\n",
       "      <td>0.071412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>9.061570e+05</td>\n",
       "      <td>13.355000</td>\n",
       "      <td>18.855000</td>\n",
       "      <td>86.210000</td>\n",
       "      <td>548.750000</td>\n",
       "      <td>0.095865</td>\n",
       "      <td>0.092525</td>\n",
       "      <td>0.061400</td>\n",
       "      <td>0.033455</td>\n",
       "      <td>0.179200</td>\n",
       "      <td>...</td>\n",
       "      <td>14.96500</td>\n",
       "      <td>25.425000</td>\n",
       "      <td>97.655000</td>\n",
       "      <td>685.550000</td>\n",
       "      <td>0.131300</td>\n",
       "      <td>0.211850</td>\n",
       "      <td>0.226550</td>\n",
       "      <td>0.099840</td>\n",
       "      <td>0.282050</td>\n",
       "      <td>0.080015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>8.825022e+06</td>\n",
       "      <td>15.780000</td>\n",
       "      <td>21.802500</td>\n",
       "      <td>103.875000</td>\n",
       "      <td>782.625000</td>\n",
       "      <td>0.105300</td>\n",
       "      <td>0.130400</td>\n",
       "      <td>0.129650</td>\n",
       "      <td>0.073730</td>\n",
       "      <td>0.195625</td>\n",
       "      <td>...</td>\n",
       "      <td>18.76750</td>\n",
       "      <td>29.757500</td>\n",
       "      <td>125.175000</td>\n",
       "      <td>1073.500000</td>\n",
       "      <td>0.146000</td>\n",
       "      <td>0.337600</td>\n",
       "      <td>0.381400</td>\n",
       "      <td>0.161325</td>\n",
       "      <td>0.317675</td>\n",
       "      <td>0.092065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>9.113205e+08</td>\n",
       "      <td>28.110000</td>\n",
       "      <td>39.280000</td>\n",
       "      <td>188.500000</td>\n",
       "      <td>2501.000000</td>\n",
       "      <td>0.163400</td>\n",
       "      <td>0.345400</td>\n",
       "      <td>0.426800</td>\n",
       "      <td>0.201200</td>\n",
       "      <td>0.304000</td>\n",
       "      <td>...</td>\n",
       "      <td>36.04000</td>\n",
       "      <td>49.540000</td>\n",
       "      <td>251.200000</td>\n",
       "      <td>4254.000000</td>\n",
       "      <td>0.222600</td>\n",
       "      <td>1.058000</td>\n",
       "      <td>1.252000</td>\n",
       "      <td>0.291000</td>\n",
       "      <td>0.663800</td>\n",
       "      <td>0.207500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             842302       17.99       10.38       122.8         1001  \\\n",
       "count  5.680000e+02  568.000000  568.000000  568.000000   568.000000   \n",
       "mean   3.042382e+07   14.120491   19.305335   91.914754   654.279754   \n",
       "std    1.251246e+08    3.523416    4.288506   24.285848   351.923751   \n",
       "min    8.670000e+03    6.981000    9.710000   43.790000   143.500000   \n",
       "25%    8.692225e+05   11.697500   16.177500   75.135000   420.175000   \n",
       "50%    9.061570e+05   13.355000   18.855000   86.210000   548.750000   \n",
       "75%    8.825022e+06   15.780000   21.802500  103.875000   782.625000   \n",
       "max    9.113205e+08   28.110000   39.280000  188.500000  2501.000000   \n",
       "\n",
       "           0.1184      0.2776      0.3001      0.1471      0.2419  ...  \\\n",
       "count  568.000000  568.000000  568.000000  568.000000  568.000000  ...   \n",
       "mean     0.096321    0.104036    0.088427    0.048746    0.181055  ...   \n",
       "std      0.014046    0.052355    0.079294    0.038617    0.027319  ...   \n",
       "min      0.052630    0.019380    0.000000    0.000000    0.106000  ...   \n",
       "25%      0.086290    0.064815    0.029540    0.020310    0.161900  ...   \n",
       "50%      0.095865    0.092525    0.061400    0.033455    0.179200  ...   \n",
       "75%      0.105300    0.130400    0.129650    0.073730    0.195625  ...   \n",
       "max      0.163400    0.345400    0.426800    0.201200    0.304000  ...   \n",
       "\n",
       "           25.38       17.33       184.6         2019      0.1622      0.6656  \\\n",
       "count  568.00000  568.000000  568.000000   568.000000  568.000000  568.000000   \n",
       "mean    16.25315   25.691919  107.125053   878.578873    0.132316    0.253541   \n",
       "std      4.82232    6.141662   33.474687   567.846267    0.022818    0.156523   \n",
       "min      7.93000   12.020000   50.410000   185.200000    0.071170    0.027290   \n",
       "25%     13.01000   21.095000   84.102500   514.975000    0.116600    0.146900   \n",
       "50%     14.96500   25.425000   97.655000   685.550000    0.131300    0.211850   \n",
       "75%     18.76750   29.757500  125.175000  1073.500000    0.146000    0.337600   \n",
       "max     36.04000   49.540000  251.200000  4254.000000    0.222600    1.058000   \n",
       "\n",
       "           0.7119      0.2654      0.4601      0.1189  \n",
       "count  568.000000  568.000000  568.000000  568.000000  \n",
       "mean     0.271414    0.114341    0.289776    0.083884  \n",
       "std      0.207989    0.065484    0.061508    0.018017  \n",
       "min      0.000000    0.000000    0.156500    0.055040  \n",
       "25%      0.114475    0.064730    0.250350    0.071412  \n",
       "50%      0.226550    0.099840    0.282050    0.080015  \n",
       "75%      0.381400    0.161325    0.317675    0.092065  \n",
       "max      1.252000    0.291000    0.663800    0.207500  \n",
       "\n",
       "[8 rows x 31 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过数据对象的describe()函数进行简单的描述性统计分析--均值，标准差，分位数\n",
    "credit_df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "67016fef-6f41-437d-99bb-99e62c648dc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "checking    0\n",
       "duration    0\n",
       "history     0\n",
       "purpose     0\n",
       "amount      0\n",
       "savings     0\n",
       "employed    0\n",
       "installp    0\n",
       "marital     0\n",
       "coapp       0\n",
       "resident    0\n",
       "property    0\n",
       "age         0\n",
       "other       0\n",
       "housing     0\n",
       "existcr     0\n",
       "job         0\n",
       "depends     0\n",
       "telephon    0\n",
       "foreign     0\n",
       "good_bad    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "credit_df.isnull().sum()     #判断有没有空值 并累加此结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "9ddcab4b-50b3-4ce9-9348-c16b03a4c47c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>checking</th>\n",
       "      <th>duration</th>\n",
       "      <th>history</th>\n",
       "      <th>purpose</th>\n",
       "      <th>amount</th>\n",
       "      <th>savings</th>\n",
       "      <th>employed</th>\n",
       "      <th>installp</th>\n",
       "      <th>marital</th>\n",
       "      <th>coapp</th>\n",
       "      <th>...</th>\n",
       "      <th>property</th>\n",
       "      <th>age</th>\n",
       "      <th>other</th>\n",
       "      <th>housing</th>\n",
       "      <th>existcr</th>\n",
       "      <th>job</th>\n",
       "      <th>depends</th>\n",
       "      <th>telephon</th>\n",
       "      <th>foreign</th>\n",
       "      <th>good_bad</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>0 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [checking, duration, history, purpose, amount, savings, employed, installp, marital, coapp, resident, property, age, other, housing, existcr, job, depends, telephon, foreign, good_bad]\n",
       "Index: []\n",
       "\n",
       "[0 rows x 21 columns]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "credit_df[credit_df.duplicated()] #查看重复数据 #对类别型变量进行频数统计"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36363b7a-5e92-46bc-a797-86de4bc78e20",
   "metadata": {},
   "source": [
    "# 2、数据准备\n",
    "- 类别型变量进行数字编码（one-hot独热编码）\n",
    "- 数据集拆分成train和test\n",
    "- 准备好X_train,y_train,X_test,y_test数据对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dad435ee-ea56-479f-9825-cd0e71e9d007",
   "metadata": {},
   "source": [
    "## 独热编码：把数值变化转换为矩阵的变化，可以让计算速度加快\n",
    "- 独热编码（One-Hot Encoding）是一种处理分类变量的方法，常用于机器学习和统计学领域。在独热编码中，每个类别值都被转换成一个二进制向量，除了表示该类别的一个位置是1以外，其余位置都是0。\n",
    "\n",
    "- 例如，假设有一个分类变量“颜色”，它有三个类别：红色、绿色和蓝色。使用独热编码，我们可以这样表示这三个类别：\n",
    "- 红色：[1, 0, 0]\n",
    "- 绿色：[0, 1, 0]\n",
    "- 蓝色：[0, 0, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "e941770d-9ce0-464a-8bff-9587ef88d619",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      1\n",
       "1      2\n",
       "2      4\n",
       "3      1\n",
       "4      1\n",
       "      ..\n",
       "995    4\n",
       "996    1\n",
       "997    4\n",
       "998    1\n",
       "999    2\n",
       "Name: checking, Length: 1000, dtype: int64"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "credit_df.checking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "2ed50b5c-797f-4b2b-a205-783a3df51601",
   "metadata": {},
   "outputs": [],
   "source": [
    "checking = pd.get_dummies(credit_df.checking,prefix='checking') # 把类别型变量进行独热编码(1->N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "2d2c6512-d41d-40a8-81c7-bb9db05cd05a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>checking_1</th>\n",
       "      <th>checking_2</th>\n",
       "      <th>checking_3</th>\n",
       "      <th>checking_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     checking_1  checking_2  checking_3  checking_4\n",
       "0          True       False       False       False\n",
       "1         False        True       False       False\n",
       "2         False       False       False        True\n",
       "3          True       False       False       False\n",
       "4          True       False       False       False\n",
       "..          ...         ...         ...         ...\n",
       "995       False       False       False        True\n",
       "996        True       False       False       False\n",
       "997       False       False       False        True\n",
       "998        True       False       False       False\n",
       "999       False        True       False       False\n",
       "\n",
       "[1000 rows x 4 columns]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "checking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "823ff922-e839-4530-a2c1-73bc454be32c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>purpose_0</th>\n",
       "      <th>purpose_1</th>\n",
       "      <th>purpose_2</th>\n",
       "      <th>purpose_3</th>\n",
       "      <th>purpose_4</th>\n",
       "      <th>purpose_5</th>\n",
       "      <th>purpose_6</th>\n",
       "      <th>purpose_8</th>\n",
       "      <th>purpose_9</th>\n",
       "      <th>purpose_X</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   purpose_0  purpose_1  purpose_2  purpose_3  purpose_4  purpose_5  \\\n",
       "0      False      False      False       True      False      False   \n",
       "1      False      False      False       True      False      False   \n",
       "2      False      False      False      False      False      False   \n",
       "3      False      False       True      False      False      False   \n",
       "4       True      False      False      False      False      False   \n",
       "\n",
       "   purpose_6  purpose_8  purpose_9  purpose_X  \n",
       "0      False      False      False      False  \n",
       "1      False      False      False      False  \n",
       "2       True      False      False      False  \n",
       "3      False      False      False      False  \n",
       "4      False      False      False      False  "
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把类别型变量进行独热编码(1->N)\n",
    "checking = pd.get_dummies(credit_df.checking,prefix='checking')\n",
    "history = pd.get_dummies(credit_df.history,prefix='history')\n",
    "purpose = pd.get_dummies(credit_df.purpose,prefix='purpose')\n",
    "savings = pd.get_dummies(credit_df.savings,prefix='savings')\n",
    "employed = pd.get_dummies(credit_df.employed,prefix='employed')\n",
    "installp = pd.get_dummies(credit_df.installp,prefix='installp')\n",
    "marital = pd.get_dummies(credit_df.marital,prefix='marital')\n",
    "coapp = pd.get_dummies(credit_df.coapp,prefix='coapp')\n",
    "installp = pd.get_dummies(credit_df.installp,prefix='installp')\n",
    "resident = pd.get_dummies(credit_df.resident,prefix='resident')\n",
    "property = pd.get_dummies(credit_df.property,prefix='property')\n",
    "housing = pd.get_dummies(credit_df.housing,prefix='housing')\n",
    "existcr = pd.get_dummies(credit_df.existcr,prefix='existcr')\n",
    "job = pd.get_dummies(credit_df.job,prefix='job')\n",
    "depends = pd.get_dummies(credit_df.depends,prefix='depends')\n",
    "telephon = pd.get_dummies(credit_df.telephon,prefix='telephon')\n",
    "foreign = pd.get_dummies(credit_df.foreign,prefix='foreign')\n",
    "\n",
    "# 随便查看一个编码后的数据\n",
    "purpose.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "a400fffd-ba43-4467-9936-1d493e0e6159",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>duration</th>\n",
       "      <th>amount</th>\n",
       "      <th>age</th>\n",
       "      <th>checking_1</th>\n",
       "      <th>checking_2</th>\n",
       "      <th>checking_3</th>\n",
       "      <th>checking_4</th>\n",
       "      <th>history_0</th>\n",
       "      <th>history_1</th>\n",
       "      <th>history_2</th>\n",
       "      <th>...</th>\n",
       "      <th>job_1</th>\n",
       "      <th>job_2</th>\n",
       "      <th>job_3</th>\n",
       "      <th>job_4</th>\n",
       "      <th>depends_1</th>\n",
       "      <th>depends_2</th>\n",
       "      <th>telephon_1</th>\n",
       "      <th>telephon_2</th>\n",
       "      <th>foreign_1</th>\n",
       "      <th>foreign_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6</td>\n",
       "      <td>1169</td>\n",
       "      <td>67</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>48</td>\n",
       "      <td>5951</td>\n",
       "      <td>22</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12</td>\n",
       "      <td>2096</td>\n",
       "      <td>49</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>42</td>\n",
       "      <td>7882</td>\n",
       "      <td>45</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>24</td>\n",
       "      <td>4870</td>\n",
       "      <td>53</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 72 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   duration  amount  age  checking_1  checking_2  checking_3  checking_4  \\\n",
       "0         6    1169   67        True       False       False       False   \n",
       "1        48    5951   22       False        True       False       False   \n",
       "2        12    2096   49       False       False       False        True   \n",
       "3        42    7882   45        True       False       False       False   \n",
       "4        24    4870   53        True       False       False       False   \n",
       "\n",
       "   history_0  history_1  history_2  ...  job_1  job_2  job_3  job_4  \\\n",
       "0      False      False      False  ...  False  False   True  False   \n",
       "1      False      False       True  ...  False  False   True  False   \n",
       "2      False      False      False  ...  False   True  False  False   \n",
       "3      False      False       True  ...  False  False   True  False   \n",
       "4      False      False      False  ...  False  False   True  False   \n",
       "\n",
       "   depends_1  depends_2  telephon_1  telephon_2  foreign_1  foreign_2  \n",
       "0       True      False       False        True       True      False  \n",
       "1       True      False        True       False       True      False  \n",
       "2      False       True        True       False       True      False  \n",
       "3      False       True        True       False       True      False  \n",
       "4      False       True        True       False       True      False  \n",
       "\n",
       "[5 rows x 72 columns]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用concat()函数把转换后的数据对象全部合并在一起变成新的数据对象，命名为trainData_X\n",
    "trainData_X = pd.concat([credit_df.duration, credit_df.amount, credit_df.age, checking, history, purpose, savings, employed, installp, marital, coapp, installp, resident, property, housing, existcr, job, depends, telephon, foreign], axis=1)\n",
    "trainData_X.head()\n",
    "# 72 columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "1e5cd1c2-f6ac-4207-8e79-d323c8189657",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "target\n",
       "0    700\n",
       "1    300\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构造目标变量y的数据对象\n",
    "# 当前的目标变量good_bad的取值是'good'和'bad'，一般我们习惯把二分类的类别值编码为0和1，1一般表示类别比较少的那一类，他们往往是我们感兴趣的\n",
    "credit_df['target'] = 0\n",
    "credit_df.loc[(credit_df.good_bad == 'bad'), 'target'] = 1\n",
    "\n",
    "trainData_y = credit_df['target'] \n",
    "trainData_y.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "821aa750-0783-4e7e-a1cb-33397b4d32d7",
   "metadata": {},
   "source": [
    "# 3、模型训练与调参"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "5a3c158a-5d24-4512-b4f3-450c6bb1d3ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_test,y_train,y_test=train_test_split(trainData_X,trainData_y,test_size=0.3,random_state=9)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "c4b2fa91-1d8d-41e1-8940-c8a011eabf2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>duration</th>\n",
       "      <th>amount</th>\n",
       "      <th>age</th>\n",
       "      <th>checking_1</th>\n",
       "      <th>checking_2</th>\n",
       "      <th>checking_3</th>\n",
       "      <th>checking_4</th>\n",
       "      <th>history_0</th>\n",
       "      <th>history_1</th>\n",
       "      <th>history_2</th>\n",
       "      <th>...</th>\n",
       "      <th>job_1</th>\n",
       "      <th>job_2</th>\n",
       "      <th>job_3</th>\n",
       "      <th>job_4</th>\n",
       "      <th>depends_1</th>\n",
       "      <th>depends_2</th>\n",
       "      <th>telephon_1</th>\n",
       "      <th>telephon_2</th>\n",
       "      <th>foreign_1</th>\n",
       "      <th>foreign_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>775</th>\n",
       "      <td>24</td>\n",
       "      <td>1371</td>\n",
       "      <td>25</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>975</th>\n",
       "      <td>24</td>\n",
       "      <td>1258</td>\n",
       "      <td>57</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>895</th>\n",
       "      <td>36</td>\n",
       "      <td>8947</td>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>337</th>\n",
       "      <td>15</td>\n",
       "      <td>1275</td>\n",
       "      <td>24</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>48</td>\n",
       "      <td>4308</td>\n",
       "      <td>24</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>950</th>\n",
       "      <td>18</td>\n",
       "      <td>3590</td>\n",
       "      <td>40</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>501</th>\n",
       "      <td>36</td>\n",
       "      <td>5493</td>\n",
       "      <td>42</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>638</th>\n",
       "      <td>12</td>\n",
       "      <td>1493</td>\n",
       "      <td>34</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>348</th>\n",
       "      <td>6</td>\n",
       "      <td>1743</td>\n",
       "      <td>34</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>382</th>\n",
       "      <td>22</td>\n",
       "      <td>1283</td>\n",
       "      <td>25</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>700 rows × 72 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     duration  amount  age  checking_1  checking_2  checking_3  checking_4  \\\n",
       "775        24    1371   25        True       False       False       False   \n",
       "975        24    1258   57       False       False        True       False   \n",
       "895        36    8947   31       False       False       False        True   \n",
       "337        15    1275   24        True       False       False       False   \n",
       "11         48    4308   24        True       False       False       False   \n",
       "..        ...     ...  ...         ...         ...         ...         ...   \n",
       "950        18    3590   40       False        True       False       False   \n",
       "501        36    5493   42        True       False       False       False   \n",
       "638        12    1493   34       False       False       False        True   \n",
       "348         6    1743   34       False       False       False        True   \n",
       "382        22    1283   25       False       False       False        True   \n",
       "\n",
       "     history_0  history_1  history_2  ...  job_1  job_2  job_3  job_4  \\\n",
       "775      False      False       True  ...  False  False   True  False   \n",
       "975      False      False       True  ...  False   True  False  False   \n",
       "895      False      False      False  ...  False  False  False   True   \n",
       "337      False      False       True  ...  False  False   True  False   \n",
       "11       False      False       True  ...  False  False   True  False   \n",
       "..         ...        ...        ...  ...    ...    ...    ...    ...   \n",
       "950      False      False      False  ...   True  False  False  False   \n",
       "501      False      False       True  ...  False  False   True  False   \n",
       "638      False      False       True  ...  False  False   True  False   \n",
       "348      False      False      False  ...  False   True  False  False   \n",
       "382      False      False       True  ...  False  False   True  False   \n",
       "\n",
       "     depends_1  depends_2  telephon_1  telephon_2  foreign_1  foreign_2  \n",
       "775       True      False        True       False       True      False  \n",
       "975       True      False        True       False       True      False  \n",
       "895      False       True       False        True       True      False  \n",
       "337       True      False        True       False       True      False  \n",
       "11        True      False        True       False       True      False  \n",
       "..         ...        ...         ...         ...        ...        ...  \n",
       "950      False       True       False        True       True      False  \n",
       "501      False       True        True       False       True      False  \n",
       "638      False       True        True       False       True      False  \n",
       "348       True      False        True       False       True      False  \n",
       "382       True      False        True       False       True      False  \n",
       "\n",
       "[700 rows x 72 columns]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "849cf518-c8bc-409c-a869-f103cb066582",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-3 {\n",
       "  /* Definition of color scheme common for light and dark mode */\n",
       "  --sklearn-color-text: black;\n",
       "  --sklearn-color-line: gray;\n",
       "  /* Definition of color scheme for unfitted estimators */\n",
       "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
       "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
       "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
       "  --sklearn-color-unfitted-level-3: chocolate;\n",
       "  /* Definition of color scheme for fitted estimators */\n",
       "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
       "  --sklearn-color-fitted-level-1: #d4ebff;\n",
       "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
       "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
       "\n",
       "  /* Specific color for light theme */\n",
       "  --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",
       "  --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-icon: #696969;\n",
       "\n",
       "  @media (prefers-color-scheme: dark) {\n",
       "    /* Redefinition of color scheme for dark theme */\n",
       "    --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",
       "    --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-icon: #878787;\n",
       "  }\n",
       "}\n",
       "\n",
       "#sk-container-id-3 {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 pre {\n",
       "  padding: 0;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 input.sk-hidden--visually {\n",
       "  border: 0;\n",
       "  clip: rect(1px 1px 1px 1px);\n",
       "  clip: rect(1px, 1px, 1px, 1px);\n",
       "  height: 1px;\n",
       "  margin: -1px;\n",
       "  overflow: hidden;\n",
       "  padding: 0;\n",
       "  position: absolute;\n",
       "  width: 1px;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-dashed-wrapped {\n",
       "  border: 1px dashed var(--sklearn-color-line);\n",
       "  margin: 0 0.4em 0.5em 0.4em;\n",
       "  box-sizing: border-box;\n",
       "  padding-bottom: 0.4em;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-container {\n",
       "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
       "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
       "     so we also need the `!important` here to be able to override the\n",
       "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
       "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
       "  display: inline-block !important;\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-text-repr-fallback {\n",
       "  display: none;\n",
       "}\n",
       "\n",
       "div.sk-parallel-item,\n",
       "div.sk-serial,\n",
       "div.sk-item {\n",
       "  /* draw centered vertical line to link estimators */\n",
       "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
       "  background-size: 2px 100%;\n",
       "  background-repeat: no-repeat;\n",
       "  background-position: center center;\n",
       "}\n",
       "\n",
       "/* Parallel-specific style estimator block */\n",
       "\n",
       "#sk-container-id-3 div.sk-parallel-item::after {\n",
       "  content: \"\";\n",
       "  width: 100%;\n",
       "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
       "  flex-grow: 1;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-parallel {\n",
       "  display: flex;\n",
       "  align-items: stretch;\n",
       "  justify-content: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-parallel-item {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-parallel-item:first-child::after {\n",
       "  align-self: flex-end;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-parallel-item:last-child::after {\n",
       "  align-self: flex-start;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-parallel-item:only-child::after {\n",
       "  width: 0;\n",
       "}\n",
       "\n",
       "/* Serial-specific style estimator block */\n",
       "\n",
       "#sk-container-id-3 div.sk-serial {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  align-items: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  padding-right: 1em;\n",
       "  padding-left: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
       "clickable and can be expanded/collapsed.\n",
       "- Pipeline and ColumnTransformer use this feature and define the default style\n",
       "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
       "*/\n",
       "\n",
       "/* Pipeline and ColumnTransformer style (default) */\n",
       "\n",
       "#sk-container-id-3 div.sk-toggleable {\n",
       "  /* Default theme specific background. It is overwritten whether we have a\n",
       "  specific estimator or a Pipeline/ColumnTransformer */\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "/* Toggleable label */\n",
       "#sk-container-id-3 label.sk-toggleable__label {\n",
       "  cursor: pointer;\n",
       "  display: block;\n",
       "  width: 100%;\n",
       "  margin-bottom: 0;\n",
       "  padding: 0.5em;\n",
       "  box-sizing: border-box;\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 label.sk-toggleable__label-arrow:before {\n",
       "  /* Arrow on the left of the label */\n",
       "  content: \"▸\";\n",
       "  float: left;\n",
       "  margin-right: 0.25em;\n",
       "  color: var(--sklearn-color-icon);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 label.sk-toggleable__label-arrow:hover:before {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "/* Toggleable content - dropdown */\n",
       "\n",
       "#sk-container-id-3 div.sk-toggleable__content {\n",
       "  max-height: 0;\n",
       "  max-width: 0;\n",
       "  overflow: hidden;\n",
       "  text-align: left;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-toggleable__content.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-toggleable__content pre {\n",
       "  margin: 0.2em;\n",
       "  border-radius: 0.25em;\n",
       "  color: var(--sklearn-color-text);\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-toggleable__content.fitted pre {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
       "  /* Expand drop-down */\n",
       "  max-height: 200px;\n",
       "  max-width: 100%;\n",
       "  overflow: auto;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
       "  content: \"▾\";\n",
       "}\n",
       "\n",
       "/* Pipeline/ColumnTransformer-specific style */\n",
       "\n",
       "#sk-container-id-3 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator-specific style */\n",
       "\n",
       "/* Colorize estimator box */\n",
       "#sk-container-id-3 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-label label.sk-toggleable__label,\n",
       "#sk-container-id-3 div.sk-label label {\n",
       "  /* The background is the default theme color */\n",
       "  color: var(--sklearn-color-text-on-default-background);\n",
       "}\n",
       "\n",
       "/* On hover, darken the color of the background */\n",
       "#sk-container-id-3 div.sk-label:hover label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "/* Label box, darken color on hover, fitted */\n",
       "#sk-container-id-3 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator label */\n",
       "\n",
       "#sk-container-id-3 div.sk-label label {\n",
       "  font-family: monospace;\n",
       "  font-weight: bold;\n",
       "  display: inline-block;\n",
       "  line-height: 1.2em;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-label-container {\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "/* Estimator-specific */\n",
       "#sk-container-id-3 div.sk-estimator {\n",
       "  font-family: monospace;\n",
       "  border: 1px dotted var(--sklearn-color-border-box);\n",
       "  border-radius: 0.25em;\n",
       "  box-sizing: border-box;\n",
       "  margin-bottom: 0.5em;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-estimator.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "/* on hover */\n",
       "#sk-container-id-3 div.sk-estimator:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-3 div.sk-estimator.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
       "\n",
       "/* Common style for \"i\" and \"?\" */\n",
       "\n",
       ".sk-estimator-doc-link,\n",
       "a:link.sk-estimator-doc-link,\n",
       "a:visited.sk-estimator-doc-link {\n",
       "  float: right;\n",
       "  font-size: smaller;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1em;\n",
       "  height: 1em;\n",
       "  width: 1em;\n",
       "  text-decoration: none !important;\n",
       "  margin-left: 1ex;\n",
       "  /* unfitted */\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted,\n",
       "a:link.sk-estimator-doc-link.fitted,\n",
       "a:visited.sk-estimator-doc-link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "/* Span, style for the box shown on hovering the info icon */\n",
       ".sk-estimator-doc-link span {\n",
       "  display: none;\n",
       "  z-index: 9999;\n",
       "  position: relative;\n",
       "  font-weight: normal;\n",
       "  right: .2ex;\n",
       "  padding: .5ex;\n",
       "  margin: .5ex;\n",
       "  width: min-content;\n",
       "  min-width: 20ex;\n",
       "  max-width: 50ex;\n",
       "  color: var(--sklearn-color-text);\n",
       "  box-shadow: 2pt 2pt 4pt #999;\n",
       "  /* unfitted */\n",
       "  background: var(--sklearn-color-unfitted-level-0);\n",
       "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted span {\n",
       "  /* fitted */\n",
       "  background: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link:hover span {\n",
       "  display: block;\n",
       "}\n",
       "\n",
       "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
       "\n",
       "#sk-container-id-3 a.estimator_doc_link {\n",
       "  float: right;\n",
       "  font-size: 1rem;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1rem;\n",
       "  height: 1rem;\n",
       "  width: 1rem;\n",
       "  text-decoration: none;\n",
       "  /* unfitted */\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 a.estimator_doc_link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "#sk-container-id-3 a.estimator_doc_link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "#sk-container-id-3 a.estimator_doc_link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "</style><div id=\"sk-container-id-3\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LogisticRegression(solver=&#x27;liblinear&#x27;)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" checked><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;&nbsp;LogisticRegression<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.linear_model.LogisticRegression.html\">?<span>Documentation for LogisticRegression</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></label><div class=\"sk-toggleable__content fitted\"><pre>LogisticRegression(solver=&#x27;liblinear&#x27;)</pre></div> </div></div></div></div>"
      ],
      "text/plain": [
       "LogisticRegression(solver='liblinear')"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "# 通过LogisticRegression类定义一个逻辑回归模型名字叫lr\n",
    "lr = LogisticRegression(solver='liblinear')\n",
    "\n",
    "# 对lr模型进行训练(fit)\n",
    "lr.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "6924f9c2-827c-463c-85bf-1c8850cea127",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.3458735]\n",
      "[[ 2.12125553e-02  1.34891563e-04 -9.19637780e-03  7.18847917e-01\n",
      "   3.79702407e-01 -3.85060843e-01 -1.05936298e+00  5.27554254e-01\n",
      "   6.15717512e-01 -2.78633587e-02 -3.65482554e-01 -1.09579935e+00\n",
      "   5.31832013e-01 -7.63349225e-01 -1.50335094e-01 -2.60492220e-01\n",
      "   1.48773460e-01 -1.12346233e-01  5.17583886e-01 -2.74126082e-01\n",
      "   3.54372656e-02 -1.88512681e-02  2.68373797e-01 -1.02565310e-01\n",
      "   1.82665057e-01 -3.15428211e-01 -3.78918832e-01  4.89864522e-02\n",
      "   3.45513071e-01  3.73276897e-02 -7.21811621e-01 -5.58890894e-02\n",
      "  -3.00034600e-01 -1.45199863e-01 -8.15460443e-02  1.80907009e-01\n",
      "   7.58941143e-02  7.46120329e-02 -4.47138944e-01 -4.92407014e-02\n",
      "   2.20795166e-01  2.73517069e-01 -8.40185733e-01 -3.00034600e-01\n",
      "  -1.45199863e-01 -8.15460443e-02  1.80907009e-01 -1.28636615e-01\n",
      "   1.71483256e-01 -1.50708112e-01 -2.38012026e-01 -4.44297779e-01\n",
      "   8.79092140e-02 -1.02199522e-01  1.12714589e-01  2.23425172e-01\n",
      "  -2.12275244e-01 -3.57023426e-01 -4.11829624e-01  1.07703302e-01\n",
      "  -1.70105109e-01  1.28357933e-01  1.94584816e-02 -4.19066823e-02\n",
      "  -4.59990571e-02 -2.77426240e-01 -3.43239679e-01 -2.63381852e-03\n",
      "  -1.15876908e-01 -2.29996589e-01  3.12529305e-01 -6.58402803e-01]]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>coef</th>\n",
       "      <th>columns</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[0.02121255532700864]</td>\n",
       "      <td>duration</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[0.0001348915629513493]</td>\n",
       "      <td>amount</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>[-0.009196377800893406]</td>\n",
       "      <td>age</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[0.7188479171900368]</td>\n",
       "      <td>checking_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>[0.37970240657607807]</td>\n",
       "      <td>checking_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>[-0.0026338185239433948]</td>\n",
       "      <td>depends_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68</th>\n",
       "      <td>[-0.11587690847455728]</td>\n",
       "      <td>telephon_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>[-0.22999658946526524]</td>\n",
       "      <td>telephon_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>[0.3125293051805936]</td>\n",
       "      <td>foreign_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>[-0.6584028031204122]</td>\n",
       "      <td>foreign_2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>72 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                        coef     columns\n",
       "0      [0.02121255532700864]    duration\n",
       "1    [0.0001348915629513493]      amount\n",
       "2    [-0.009196377800893406]         age\n",
       "3       [0.7188479171900368]  checking_1\n",
       "4      [0.37970240657607807]  checking_2\n",
       "..                       ...         ...\n",
       "67  [-0.0026338185239433948]   depends_2\n",
       "68    [-0.11587690847455728]  telephon_1\n",
       "69    [-0.22999658946526524]  telephon_2\n",
       "70      [0.3125293051805936]   foreign_1\n",
       "71     [-0.6584028031204122]   foreign_2\n",
       "\n",
       "[72 rows x 2 columns]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 查看模型结果\n",
    "print(lr.intercept_ )\n",
    "print(lr.coef_)\n",
    "\n",
    "# 把变量名称和系数对应在一起方便查看\n",
    "pd.DataFrame(list(zip(np.transpose(lr.coef_), X_train.columns)), columns=['coef', 'columns'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "0a51d0f7-0f56-471b-b9e0-e943fcf83478",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LogisticRegression(C=0.1, solver='liblinear')\n",
      "0.7585714285714286\n",
      "{'C': 0.1, 'penalty': 'l2'}\n"
     ]
    }
   ],
   "source": [
    "# grid search调参\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "parameters = {\n",
    "    'penalty': ('l1', 'l2'),\n",
    "    'C': (0.01, 0.1, 1, 10),\n",
    "}\n",
    "\n",
    "lr = LogisticRegression(solver='liblinear')\n",
    "lr_search = GridSearchCV(lr, parameters, scoring='accuracy', cv=5)\n",
    "lr_search.fit(X_train, y_train)\n",
    "\n",
    "#查看最佳结果\n",
    "print(lr_search.best_estimator_)\n",
    "print(lr_search.best_score_)\n",
    "print(lr_search.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "56239588-4b05-4aa2-992b-ab525b8c6920",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-4 {\n",
       "  /* Definition of color scheme common for light and dark mode */\n",
       "  --sklearn-color-text: black;\n",
       "  --sklearn-color-line: gray;\n",
       "  /* Definition of color scheme for unfitted estimators */\n",
       "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
       "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
       "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
       "  --sklearn-color-unfitted-level-3: chocolate;\n",
       "  /* Definition of color scheme for fitted estimators */\n",
       "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
       "  --sklearn-color-fitted-level-1: #d4ebff;\n",
       "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
       "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
       "\n",
       "  /* Specific color for light theme */\n",
       "  --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",
       "  --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-icon: #696969;\n",
       "\n",
       "  @media (prefers-color-scheme: dark) {\n",
       "    /* Redefinition of color scheme for dark theme */\n",
       "    --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",
       "    --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-icon: #878787;\n",
       "  }\n",
       "}\n",
       "\n",
       "#sk-container-id-4 {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 pre {\n",
       "  padding: 0;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 input.sk-hidden--visually {\n",
       "  border: 0;\n",
       "  clip: rect(1px 1px 1px 1px);\n",
       "  clip: rect(1px, 1px, 1px, 1px);\n",
       "  height: 1px;\n",
       "  margin: -1px;\n",
       "  overflow: hidden;\n",
       "  padding: 0;\n",
       "  position: absolute;\n",
       "  width: 1px;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-dashed-wrapped {\n",
       "  border: 1px dashed var(--sklearn-color-line);\n",
       "  margin: 0 0.4em 0.5em 0.4em;\n",
       "  box-sizing: border-box;\n",
       "  padding-bottom: 0.4em;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-container {\n",
       "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
       "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
       "     so we also need the `!important` here to be able to override the\n",
       "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
       "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
       "  display: inline-block !important;\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-text-repr-fallback {\n",
       "  display: none;\n",
       "}\n",
       "\n",
       "div.sk-parallel-item,\n",
       "div.sk-serial,\n",
       "div.sk-item {\n",
       "  /* draw centered vertical line to link estimators */\n",
       "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
       "  background-size: 2px 100%;\n",
       "  background-repeat: no-repeat;\n",
       "  background-position: center center;\n",
       "}\n",
       "\n",
       "/* Parallel-specific style estimator block */\n",
       "\n",
       "#sk-container-id-4 div.sk-parallel-item::after {\n",
       "  content: \"\";\n",
       "  width: 100%;\n",
       "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
       "  flex-grow: 1;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-parallel {\n",
       "  display: flex;\n",
       "  align-items: stretch;\n",
       "  justify-content: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-parallel-item {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-parallel-item:first-child::after {\n",
       "  align-self: flex-end;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-parallel-item:last-child::after {\n",
       "  align-self: flex-start;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-parallel-item:only-child::after {\n",
       "  width: 0;\n",
       "}\n",
       "\n",
       "/* Serial-specific style estimator block */\n",
       "\n",
       "#sk-container-id-4 div.sk-serial {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  align-items: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  padding-right: 1em;\n",
       "  padding-left: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
       "clickable and can be expanded/collapsed.\n",
       "- Pipeline and ColumnTransformer use this feature and define the default style\n",
       "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
       "*/\n",
       "\n",
       "/* Pipeline and ColumnTransformer style (default) */\n",
       "\n",
       "#sk-container-id-4 div.sk-toggleable {\n",
       "  /* Default theme specific background. It is overwritten whether we have a\n",
       "  specific estimator or a Pipeline/ColumnTransformer */\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "/* Toggleable label */\n",
       "#sk-container-id-4 label.sk-toggleable__label {\n",
       "  cursor: pointer;\n",
       "  display: block;\n",
       "  width: 100%;\n",
       "  margin-bottom: 0;\n",
       "  padding: 0.5em;\n",
       "  box-sizing: border-box;\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 label.sk-toggleable__label-arrow:before {\n",
       "  /* Arrow on the left of the label */\n",
       "  content: \"▸\";\n",
       "  float: left;\n",
       "  margin-right: 0.25em;\n",
       "  color: var(--sklearn-color-icon);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 label.sk-toggleable__label-arrow:hover:before {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "/* Toggleable content - dropdown */\n",
       "\n",
       "#sk-container-id-4 div.sk-toggleable__content {\n",
       "  max-height: 0;\n",
       "  max-width: 0;\n",
       "  overflow: hidden;\n",
       "  text-align: left;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-toggleable__content.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-toggleable__content pre {\n",
       "  margin: 0.2em;\n",
       "  border-radius: 0.25em;\n",
       "  color: var(--sklearn-color-text);\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-toggleable__content.fitted pre {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
       "  /* Expand drop-down */\n",
       "  max-height: 200px;\n",
       "  max-width: 100%;\n",
       "  overflow: auto;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
       "  content: \"▾\";\n",
       "}\n",
       "\n",
       "/* Pipeline/ColumnTransformer-specific style */\n",
       "\n",
       "#sk-container-id-4 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator-specific style */\n",
       "\n",
       "/* Colorize estimator box */\n",
       "#sk-container-id-4 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-label label.sk-toggleable__label,\n",
       "#sk-container-id-4 div.sk-label label {\n",
       "  /* The background is the default theme color */\n",
       "  color: var(--sklearn-color-text-on-default-background);\n",
       "}\n",
       "\n",
       "/* On hover, darken the color of the background */\n",
       "#sk-container-id-4 div.sk-label:hover label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "/* Label box, darken color on hover, fitted */\n",
       "#sk-container-id-4 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator label */\n",
       "\n",
       "#sk-container-id-4 div.sk-label label {\n",
       "  font-family: monospace;\n",
       "  font-weight: bold;\n",
       "  display: inline-block;\n",
       "  line-height: 1.2em;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-label-container {\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "/* Estimator-specific */\n",
       "#sk-container-id-4 div.sk-estimator {\n",
       "  font-family: monospace;\n",
       "  border: 1px dotted var(--sklearn-color-border-box);\n",
       "  border-radius: 0.25em;\n",
       "  box-sizing: border-box;\n",
       "  margin-bottom: 0.5em;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-estimator.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "/* on hover */\n",
       "#sk-container-id-4 div.sk-estimator:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-4 div.sk-estimator.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
       "\n",
       "/* Common style for \"i\" and \"?\" */\n",
       "\n",
       ".sk-estimator-doc-link,\n",
       "a:link.sk-estimator-doc-link,\n",
       "a:visited.sk-estimator-doc-link {\n",
       "  float: right;\n",
       "  font-size: smaller;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1em;\n",
       "  height: 1em;\n",
       "  width: 1em;\n",
       "  text-decoration: none !important;\n",
       "  margin-left: 1ex;\n",
       "  /* unfitted */\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted,\n",
       "a:link.sk-estimator-doc-link.fitted,\n",
       "a:visited.sk-estimator-doc-link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "/* Span, style for the box shown on hovering the info icon */\n",
       ".sk-estimator-doc-link span {\n",
       "  display: none;\n",
       "  z-index: 9999;\n",
       "  position: relative;\n",
       "  font-weight: normal;\n",
       "  right: .2ex;\n",
       "  padding: .5ex;\n",
       "  margin: .5ex;\n",
       "  width: min-content;\n",
       "  min-width: 20ex;\n",
       "  max-width: 50ex;\n",
       "  color: var(--sklearn-color-text);\n",
       "  box-shadow: 2pt 2pt 4pt #999;\n",
       "  /* unfitted */\n",
       "  background: var(--sklearn-color-unfitted-level-0);\n",
       "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted span {\n",
       "  /* fitted */\n",
       "  background: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link:hover span {\n",
       "  display: block;\n",
       "}\n",
       "\n",
       "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
       "\n",
       "#sk-container-id-4 a.estimator_doc_link {\n",
       "  float: right;\n",
       "  font-size: 1rem;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1rem;\n",
       "  height: 1rem;\n",
       "  width: 1rem;\n",
       "  text-decoration: none;\n",
       "  /* unfitted */\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 a.estimator_doc_link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "#sk-container-id-4 a.estimator_doc_link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "#sk-container-id-4 a.estimator_doc_link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "</style><div id=\"sk-container-id-4\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LogisticRegression(C=1, solver=&#x27;liblinear&#x27;)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-4\" type=\"checkbox\" checked><label for=\"sk-estimator-id-4\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;&nbsp;LogisticRegression<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.linear_model.LogisticRegression.html\">?<span>Documentation for LogisticRegression</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></label><div class=\"sk-toggleable__content fitted\"><pre>LogisticRegression(C=1, solver=&#x27;liblinear&#x27;)</pre></div> </div></div></div></div>"
      ],
      "text/plain": [
       "LogisticRegression(C=1, solver='liblinear')"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用最佳参数重新训练模型\n",
    "lr = LogisticRegression(C=1,penalty='l2',solver='liblinear')\n",
    "lr.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e87db368-5399-458d-a613-6516afa60113",
   "metadata": {},
   "source": [
    "# 4、模型评估 重点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "7f7278d3-74eb-4bda-94d3-5e069fb3998e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.81      0.87      0.84       218\n",
      "           1       0.57      0.48      0.52        82\n",
      "\n",
      "    accuracy                           0.76       300\n",
      "   macro avg       0.69      0.67      0.68       300\n",
      "weighted avg       0.75      0.76      0.75       300\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import classification_report\n",
    "from sklearn.metrics import confusion_matrix\n",
    "\n",
    "# 利用模型对测试集进行预测，输出target预测标签值和概率\n",
    "y_test_pred = lr.predict(X_test)\n",
    "y_test_prob = lr.predict_proba(X_test)\n",
    "\n",
    "# 分类评估汇总报告classification_report\n",
    "print(classification_report(y_test,y_test_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "48a5321f-d4dd-4b0e-90b5-9f7d4f2ef4b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[189  29]\n",
      " [ 43  39]]\n"
     ]
    }
   ],
   "source": [
    "# 误分类矩阵 confusion_matrix\n",
    "print(confusion_matrix(y_test,y_test_pred))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6bdbbfd-6a1b-4240-acf3-d1421539c838",
   "metadata": {},
   "source": [
    "|       |0    |1    |  |  |\n",
    "|-------|-----|-----|----|---|\n",
    "| 0     |178  |32   |TP  |TN  |\n",
    "| 1     |39   |51   |FP  |FN  |\n",
    "\n",
    "数据准备（读入数据，检查数据，One-hot，检查Target）\n",
    "构建数据集（train_test_split 训练集，测试集）\n",
    "建模\n",
    "评价"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65284e2f-326d-45df-872a-d91905bb1361",
   "metadata": {},
   "source": [
    "# 作业资料"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "caddba03-6ed8-4f79-a9ee-558a6bc96ad5",
   "metadata": {},
   "source": [
    "翻译\n",
    "---\n",
    "\n",
    "**1. 标题：威斯康星州诊断性乳腺癌（WDBC）**\n",
    "\n",
    "**2. 来源信息**\n",
    "\n",
    "a) 创作者：\n",
    "\n",
    "- 威廉·H·沃尔伯格博士，威斯康星大学普通外科系，临床科学中心，麦迪逊，威斯康星州 53792\n",
    "  邮箱：wolberg@eagle.surgery.wisc.edu\n",
    "\n",
    "- W.尼克·斯特里特，威斯康星大学计算机科学系，1210 West Dayton St.，麦迪逊，威斯康星州 53706\n",
    "  电话：608-262-6619\n",
    "  邮箱：street@cs.wisc.edu\n",
    "\n",
    "- 奥维·L·曼加萨里亚，威斯康星大学计算机科学系，1210 West Dayton St.，麦迪逊，威斯康星州 53706\n",
    "  邮箱：olvi@cs.wisc.edu\n",
    "\n",
    "b) 捐赠者：尼克·斯特里特\n",
    "\n",
    "c) 日期：1995年11月\n",
    "\n",
    "**3. 过去使用情况：**\n",
    "\n",
    "首次使用：\n",
    "\n",
    "- W.N. Street, W.H. Wolberg 和 O.L. Mangasarian\n",
    "  核特征提取用于乳腺肿瘤诊断。\n",
    "  IS&T/SPIE 1993 国际电子成像研讨会：科学与技术，卷1905，页861-870，圣何塞，加利福尼亚州，1993年。\n",
    "\n",
    "或文献：\n",
    "\n",
    "- O.L. Mangasarian, W.N. Street 和 W.H. Wolberg.\n",
    "  通过线性规划进行乳腺癌诊断和预后。\n",
    "  运筹学研究，43(4)，页570-577，7-8月1995年。\n",
    "\n",
    "医学文献：\n",
    "\n",
    "- W.H. Wolberg, W.N. Street 和 O.L. Mangasarian.\n",
    "  从细针抽吸物中应用机器学习技术诊断乳腺癌。\n",
    "  癌症信函77 (1994) 163-171。\n",
    "\n",
    "- W.H. Wolberg, W.N. Street 和 O.L. Mangasarian.\n",
    "  应用于乳腺癌诊断和预后的图像分析和机器学习。\n",
    "  分析和定量细胞学与组织学，第17卷，第2期，页77-87，4月1995年。\n",
    "\n",
    "- W.H. Wolberg, W.N. Street, D.M. Heisey 和 O.L. Mangasarian.\n",
    "  从细针抽吸物中计算机化乳腺癌诊断和预后。\n",
    "  外科档案1995;130:511-516。\n",
    "\n",
    "- W.H. Wolberg, W.N. Street, D.M. Heisey 和 O.L. Mangasarian.\n",
    "  计算机衍生的核特征区分恶性和良性乳腺细胞学。\n",
    "  人类病理学，26:792-796，1995年。\n",
    "\n",
    "参考链接：\n",
    "[http://www.cs.wisc.edu/~olvi/uwmp/mpml.html](http://www.cs.wisc.edu/~olvi/uwmp/mpml.html) \n",
    "[http://www.cs.wisc.edu/~olvi/uwmp/cancer.html](http://www.cs.wisc.edu/~olvi/uwmp/cancer.html) \n",
    "\n",
    "结果：\n",
    "\n",
    "- 预测字段2，诊断：B = 良性，M = 恶性\n",
    "- 集合使用所有30个输入特征线性可分\n",
    "- 最佳预测准确率使用一个分离平面在三维空间中最差区域、最差平滑度和平均纹理中获得。使用重复的10折交叉验证，估计准确率为97.5%。截至1995年11月，分类器已正确诊断176例连续新患者。\n",
    "\n",
    "**4. 相关信息**\n",
    "\n",
    "特征是从乳腺肿块的细针抽吸（FNA）的数字化图像中计算得出的。它们描述了图像中存在的细胞核的特征。一些图像可以在以下网址找到：\n",
    "[http://www.cs.wisc.edu/~street/images/](http://www.cs.wisc.edu/~street/images/)\n",
    "\n",
    "上述分离平面是使用多表面方法-树（MSM-T）[K. P. Bennett, \"通过线性规划构建决策树。\" 中西部人工智能和认知科学学会第四届年会，页97-101，1992]获得的，这是一种使用线性规划构建决策树的分类方法。使用1-4个特征和1-3个分离平面的穷举搜索选择了相关特征。\n",
    "\n",
    "用于获得三维空间中分离平面的实际线性规划描述在：\n",
    "[K. P. Bennett 和 O. L. Mangasarian: \"鲁棒线性规划区分两个线性不可分集合\"，优化方法和软件1，1992，23-34]。\n",
    "\n",
    "这个数据库也可以通过威斯康星大学计算机科学系ftp服务器获得：\n",
    "\n",
    "ftp ftp.cs.wisc.edu\n",
    "cd math-prog/cpo-dataset/machine-learn/WDBC/\n",
    "\n",
    "**5. 实例数量：569**\n",
    "\n",
    "**6. 属性数量：32（ID，诊断，30个实值输入特征）**\n",
    "\n",
    "**7. 属性信息**\n",
    "\n",
    "1) ID编号\n",
    "2) 诊断（M = 恶性，B = 良性）\n",
    "3-32)\n",
    "\n",
    "每个细胞核计算了十个实值特征：\n",
    "\n",
    "a) 半径（中心到周长各点距离的平均值）\n",
    "b) 纹理（灰度值的标准差）\n",
    "c) 周长\n",
    "d) 面积\n",
    "e) 平滑度（半径长度的局部变化）\n",
    "f) 紧凑度（周长^2 / 面积 - 1.0）\n",
    "g) 凹度（轮廓凹部的严重程度）\n",
    "h) 凹点（轮廓凹部的数量）\n",
    "i) 对称性\n",
    "j) 分形维数（“海岸线近似” - 1）\n",
    "\n",
    "上述列出的一些论文中包含了这些特征如何计算的详细描述。\n",
    "\n",
    "对于每个图像，计算了这些特征的平均值、标准误差和“最差”或最大值（三个最大值的平均值），结果得到30个特征。例如，字段3是平均半径，字段13是半径的标准误差，字段23是最差半径。\n",
    "\n",
    "所有特征值都重新编码为四位有效数字。\n",
    "\n",
    "**8. 缺失属性值：无**\n",
    "\n",
    "**9. 类别分布：357个良性，212个恶性**\n",
    "\n",
    "---\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "462ca808-4e19-4d94-8ac8-386439c2965d",
   "metadata": {},
   "source": [
    "# 作业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2860f417-da53-4ce5-a284-267893158154",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split  \n",
    "from sklearn.metrics import classification_report    \n",
    "from sklearn.metrics import confusion_matrix   \n",
    "from sklearn.metrics import roc_curve  \n",
    "from sklearn.metrics import auc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fac06013-61b8-414a-b874-5139ae06b05d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>842302</th>\n",
       "      <th>M</th>\n",
       "      <th>17.99</th>\n",
       "      <th>10.38</th>\n",
       "      <th>122.8</th>\n",
       "      <th>1001</th>\n",
       "      <th>0.1184</th>\n",
       "      <th>0.2776</th>\n",
       "      <th>0.3001</th>\n",
       "      <th>0.1471</th>\n",
       "      <th>...</th>\n",
       "      <th>25.38</th>\n",
       "      <th>17.33</th>\n",
       "      <th>184.6</th>\n",
       "      <th>2019</th>\n",
       "      <th>0.1622</th>\n",
       "      <th>0.6656</th>\n",
       "      <th>0.7119</th>\n",
       "      <th>0.2654</th>\n",
       "      <th>0.4601</th>\n",
       "      <th>0.1189</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>842517</td>\n",
       "      <td>M</td>\n",
       "      <td>20.57</td>\n",
       "      <td>17.77</td>\n",
       "      <td>132.90</td>\n",
       "      <td>1326.0</td>\n",
       "      <td>0.08474</td>\n",
       "      <td>0.07864</td>\n",
       "      <td>0.08690</td>\n",
       "      <td>0.07017</td>\n",
       "      <td>...</td>\n",
       "      <td>24.990</td>\n",
       "      <td>23.41</td>\n",
       "      <td>158.80</td>\n",
       "      <td>1956.0</td>\n",
       "      <td>0.12380</td>\n",
       "      <td>0.18660</td>\n",
       "      <td>0.2416</td>\n",
       "      <td>0.1860</td>\n",
       "      <td>0.2750</td>\n",
       "      <td>0.08902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>84300903</td>\n",
       "      <td>M</td>\n",
       "      <td>19.69</td>\n",
       "      <td>21.25</td>\n",
       "      <td>130.00</td>\n",
       "      <td>1203.0</td>\n",
       "      <td>0.10960</td>\n",
       "      <td>0.15990</td>\n",
       "      <td>0.19740</td>\n",
       "      <td>0.12790</td>\n",
       "      <td>...</td>\n",
       "      <td>23.570</td>\n",
       "      <td>25.53</td>\n",
       "      <td>152.50</td>\n",
       "      <td>1709.0</td>\n",
       "      <td>0.14440</td>\n",
       "      <td>0.42450</td>\n",
       "      <td>0.4504</td>\n",
       "      <td>0.2430</td>\n",
       "      <td>0.3613</td>\n",
       "      <td>0.08758</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>84348301</td>\n",
       "      <td>M</td>\n",
       "      <td>11.42</td>\n",
       "      <td>20.38</td>\n",
       "      <td>77.58</td>\n",
       "      <td>386.1</td>\n",
       "      <td>0.14250</td>\n",
       "      <td>0.28390</td>\n",
       "      <td>0.24140</td>\n",
       "      <td>0.10520</td>\n",
       "      <td>...</td>\n",
       "      <td>14.910</td>\n",
       "      <td>26.50</td>\n",
       "      <td>98.87</td>\n",
       "      <td>567.7</td>\n",
       "      <td>0.20980</td>\n",
       "      <td>0.86630</td>\n",
       "      <td>0.6869</td>\n",
       "      <td>0.2575</td>\n",
       "      <td>0.6638</td>\n",
       "      <td>0.17300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>84358402</td>\n",
       "      <td>M</td>\n",
       "      <td>20.29</td>\n",
       "      <td>14.34</td>\n",
       "      <td>135.10</td>\n",
       "      <td>1297.0</td>\n",
       "      <td>0.10030</td>\n",
       "      <td>0.13280</td>\n",
       "      <td>0.19800</td>\n",
       "      <td>0.10430</td>\n",
       "      <td>...</td>\n",
       "      <td>22.540</td>\n",
       "      <td>16.67</td>\n",
       "      <td>152.20</td>\n",
       "      <td>1575.0</td>\n",
       "      <td>0.13740</td>\n",
       "      <td>0.20500</td>\n",
       "      <td>0.4000</td>\n",
       "      <td>0.1625</td>\n",
       "      <td>0.2364</td>\n",
       "      <td>0.07678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>843786</td>\n",
       "      <td>M</td>\n",
       "      <td>12.45</td>\n",
       "      <td>15.70</td>\n",
       "      <td>82.57</td>\n",
       "      <td>477.1</td>\n",
       "      <td>0.12780</td>\n",
       "      <td>0.17000</td>\n",
       "      <td>0.15780</td>\n",
       "      <td>0.08089</td>\n",
       "      <td>...</td>\n",
       "      <td>15.470</td>\n",
       "      <td>23.75</td>\n",
       "      <td>103.40</td>\n",
       "      <td>741.6</td>\n",
       "      <td>0.17910</td>\n",
       "      <td>0.52490</td>\n",
       "      <td>0.5355</td>\n",
       "      <td>0.1741</td>\n",
       "      <td>0.3985</td>\n",
       "      <td>0.12440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>563</th>\n",
       "      <td>926424</td>\n",
       "      <td>M</td>\n",
       "      <td>21.56</td>\n",
       "      <td>22.39</td>\n",
       "      <td>142.00</td>\n",
       "      <td>1479.0</td>\n",
       "      <td>0.11100</td>\n",
       "      <td>0.11590</td>\n",
       "      <td>0.24390</td>\n",
       "      <td>0.13890</td>\n",
       "      <td>...</td>\n",
       "      <td>25.450</td>\n",
       "      <td>26.40</td>\n",
       "      <td>166.10</td>\n",
       "      <td>2027.0</td>\n",
       "      <td>0.14100</td>\n",
       "      <td>0.21130</td>\n",
       "      <td>0.4107</td>\n",
       "      <td>0.2216</td>\n",
       "      <td>0.2060</td>\n",
       "      <td>0.07115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>564</th>\n",
       "      <td>926682</td>\n",
       "      <td>M</td>\n",
       "      <td>20.13</td>\n",
       "      <td>28.25</td>\n",
       "      <td>131.20</td>\n",
       "      <td>1261.0</td>\n",
       "      <td>0.09780</td>\n",
       "      <td>0.10340</td>\n",
       "      <td>0.14400</td>\n",
       "      <td>0.09791</td>\n",
       "      <td>...</td>\n",
       "      <td>23.690</td>\n",
       "      <td>38.25</td>\n",
       "      <td>155.00</td>\n",
       "      <td>1731.0</td>\n",
       "      <td>0.11660</td>\n",
       "      <td>0.19220</td>\n",
       "      <td>0.3215</td>\n",
       "      <td>0.1628</td>\n",
       "      <td>0.2572</td>\n",
       "      <td>0.06637</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>565</th>\n",
       "      <td>926954</td>\n",
       "      <td>M</td>\n",
       "      <td>16.60</td>\n",
       "      <td>28.08</td>\n",
       "      <td>108.30</td>\n",
       "      <td>858.1</td>\n",
       "      <td>0.08455</td>\n",
       "      <td>0.10230</td>\n",
       "      <td>0.09251</td>\n",
       "      <td>0.05302</td>\n",
       "      <td>...</td>\n",
       "      <td>18.980</td>\n",
       "      <td>34.12</td>\n",
       "      <td>126.70</td>\n",
       "      <td>1124.0</td>\n",
       "      <td>0.11390</td>\n",
       "      <td>0.30940</td>\n",
       "      <td>0.3403</td>\n",
       "      <td>0.1418</td>\n",
       "      <td>0.2218</td>\n",
       "      <td>0.07820</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>566</th>\n",
       "      <td>927241</td>\n",
       "      <td>M</td>\n",
       "      <td>20.60</td>\n",
       "      <td>29.33</td>\n",
       "      <td>140.10</td>\n",
       "      <td>1265.0</td>\n",
       "      <td>0.11780</td>\n",
       "      <td>0.27700</td>\n",
       "      <td>0.35140</td>\n",
       "      <td>0.15200</td>\n",
       "      <td>...</td>\n",
       "      <td>25.740</td>\n",
       "      <td>39.42</td>\n",
       "      <td>184.60</td>\n",
       "      <td>1821.0</td>\n",
       "      <td>0.16500</td>\n",
       "      <td>0.86810</td>\n",
       "      <td>0.9387</td>\n",
       "      <td>0.2650</td>\n",
       "      <td>0.4087</td>\n",
       "      <td>0.12400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>567</th>\n",
       "      <td>92751</td>\n",
       "      <td>B</td>\n",
       "      <td>7.76</td>\n",
       "      <td>24.54</td>\n",
       "      <td>47.92</td>\n",
       "      <td>181.0</td>\n",
       "      <td>0.05263</td>\n",
       "      <td>0.04362</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>...</td>\n",
       "      <td>9.456</td>\n",
       "      <td>30.37</td>\n",
       "      <td>59.16</td>\n",
       "      <td>268.6</td>\n",
       "      <td>0.08996</td>\n",
       "      <td>0.06444</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2871</td>\n",
       "      <td>0.07039</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>568 rows × 32 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       842302  M  17.99  10.38   122.8    1001   0.1184   0.2776   0.3001  \\\n",
       "0      842517  M  20.57  17.77  132.90  1326.0  0.08474  0.07864  0.08690   \n",
       "1    84300903  M  19.69  21.25  130.00  1203.0  0.10960  0.15990  0.19740   \n",
       "2    84348301  M  11.42  20.38   77.58   386.1  0.14250  0.28390  0.24140   \n",
       "3    84358402  M  20.29  14.34  135.10  1297.0  0.10030  0.13280  0.19800   \n",
       "4      843786  M  12.45  15.70   82.57   477.1  0.12780  0.17000  0.15780   \n",
       "..        ... ..    ...    ...     ...     ...      ...      ...      ...   \n",
       "563    926424  M  21.56  22.39  142.00  1479.0  0.11100  0.11590  0.24390   \n",
       "564    926682  M  20.13  28.25  131.20  1261.0  0.09780  0.10340  0.14400   \n",
       "565    926954  M  16.60  28.08  108.30   858.1  0.08455  0.10230  0.09251   \n",
       "566    927241  M  20.60  29.33  140.10  1265.0  0.11780  0.27700  0.35140   \n",
       "567     92751  B   7.76  24.54   47.92   181.0  0.05263  0.04362  0.00000   \n",
       "\n",
       "      0.1471  ...   25.38  17.33   184.6    2019   0.1622   0.6656  0.7119  \\\n",
       "0    0.07017  ...  24.990  23.41  158.80  1956.0  0.12380  0.18660  0.2416   \n",
       "1    0.12790  ...  23.570  25.53  152.50  1709.0  0.14440  0.42450  0.4504   \n",
       "2    0.10520  ...  14.910  26.50   98.87   567.7  0.20980  0.86630  0.6869   \n",
       "3    0.10430  ...  22.540  16.67  152.20  1575.0  0.13740  0.20500  0.4000   \n",
       "4    0.08089  ...  15.470  23.75  103.40   741.6  0.17910  0.52490  0.5355   \n",
       "..       ...  ...     ...    ...     ...     ...      ...      ...     ...   \n",
       "563  0.13890  ...  25.450  26.40  166.10  2027.0  0.14100  0.21130  0.4107   \n",
       "564  0.09791  ...  23.690  38.25  155.00  1731.0  0.11660  0.19220  0.3215   \n",
       "565  0.05302  ...  18.980  34.12  126.70  1124.0  0.11390  0.30940  0.3403   \n",
       "566  0.15200  ...  25.740  39.42  184.60  1821.0  0.16500  0.86810  0.9387   \n",
       "567  0.00000  ...   9.456  30.37   59.16   268.6  0.08996  0.06444  0.0000   \n",
       "\n",
       "     0.2654  0.4601   0.1189  \n",
       "0    0.1860  0.2750  0.08902  \n",
       "1    0.2430  0.3613  0.08758  \n",
       "2    0.2575  0.6638  0.17300  \n",
       "3    0.1625  0.2364  0.07678  \n",
       "4    0.1741  0.3985  0.12440  \n",
       "..      ...     ...      ...  \n",
       "563  0.2216  0.2060  0.07115  \n",
       "564  0.1628  0.2572  0.06637  \n",
       "565  0.1418  0.2218  0.07820  \n",
       "566  0.2650  0.4087  0.12400  \n",
       "567  0.0000  0.2871  0.07039  \n",
       "\n",
       "[568 rows x 32 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "credit_df = pd.read_csv(\"D:/work/homework/数据20241121.txt\")\n",
    "credit_df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ca77e50-9744-4301-b827-c02964fb7f4d",
   "metadata": {},
   "source": [
    "## 数据理解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1360c52c-395e-4757-9da1-528f6b0d38db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 568 entries, 0 to 567\n",
      "Data columns (total 32 columns):\n",
      " #   Column    Non-Null Count  Dtype  \n",
      "---  ------    --------------  -----  \n",
      " 0   842302    568 non-null    int64  \n",
      " 1   M         568 non-null    object \n",
      " 2   17.99     568 non-null    float64\n",
      " 3   10.38     568 non-null    float64\n",
      " 4   122.8     568 non-null    float64\n",
      " 5   1001      568 non-null    float64\n",
      " 6   0.1184    568 non-null    float64\n",
      " 7   0.2776    568 non-null    float64\n",
      " 8   0.3001    568 non-null    float64\n",
      " 9   0.1471    568 non-null    float64\n",
      " 10  0.2419    568 non-null    float64\n",
      " 11  0.07871   568 non-null    float64\n",
      " 12  1.095     568 non-null    float64\n",
      " 13  0.9053    568 non-null    float64\n",
      " 14  8.589     568 non-null    float64\n",
      " 15  153.4     568 non-null    float64\n",
      " 16  0.006399  568 non-null    float64\n",
      " 17  0.04904   568 non-null    float64\n",
      " 18  0.05373   568 non-null    float64\n",
      " 19  0.01587   568 non-null    float64\n",
      " 20  0.03003   568 non-null    float64\n",
      " 21  0.006193  568 non-null    float64\n",
      " 22  25.38     568 non-null    float64\n",
      " 23  17.33     568 non-null    float64\n",
      " 24  184.6     568 non-null    float64\n",
      " 25  2019      568 non-null    float64\n",
      " 26  0.1622    568 non-null    float64\n",
      " 27  0.6656    568 non-null    float64\n",
      " 28  0.7119    568 non-null    float64\n",
      " 29  0.2654    568 non-null    float64\n",
      " 30  0.4601    568 non-null    float64\n",
      " 31  0.1189    568 non-null    float64\n",
      "dtypes: float64(30), int64(1), object(1)\n",
      "memory usage: 142.1+ KB\n"
     ]
    }
   ],
   "source": [
    "credit_df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "29d3d98c-48bd-4bc3-bb38-17df5a2eefa3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>842302</th>\n",
       "      <th>17.99</th>\n",
       "      <th>10.38</th>\n",
       "      <th>122.8</th>\n",
       "      <th>1001</th>\n",
       "      <th>0.1184</th>\n",
       "      <th>0.2776</th>\n",
       "      <th>0.3001</th>\n",
       "      <th>0.1471</th>\n",
       "      <th>0.2419</th>\n",
       "      <th>...</th>\n",
       "      <th>25.38</th>\n",
       "      <th>17.33</th>\n",
       "      <th>184.6</th>\n",
       "      <th>2019</th>\n",
       "      <th>0.1622</th>\n",
       "      <th>0.6656</th>\n",
       "      <th>0.7119</th>\n",
       "      <th>0.2654</th>\n",
       "      <th>0.4601</th>\n",
       "      <th>0.1189</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>5.680000e+02</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>568.00000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "      <td>568.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>3.042382e+07</td>\n",
       "      <td>14.120491</td>\n",
       "      <td>19.305335</td>\n",
       "      <td>91.914754</td>\n",
       "      <td>654.279754</td>\n",
       "      <td>0.096321</td>\n",
       "      <td>0.104036</td>\n",
       "      <td>0.088427</td>\n",
       "      <td>0.048746</td>\n",
       "      <td>0.181055</td>\n",
       "      <td>...</td>\n",
       "      <td>16.25315</td>\n",
       "      <td>25.691919</td>\n",
       "      <td>107.125053</td>\n",
       "      <td>878.578873</td>\n",
       "      <td>0.132316</td>\n",
       "      <td>0.253541</td>\n",
       "      <td>0.271414</td>\n",
       "      <td>0.114341</td>\n",
       "      <td>0.289776</td>\n",
       "      <td>0.083884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>1.251246e+08</td>\n",
       "      <td>3.523416</td>\n",
       "      <td>4.288506</td>\n",
       "      <td>24.285848</td>\n",
       "      <td>351.923751</td>\n",
       "      <td>0.014046</td>\n",
       "      <td>0.052355</td>\n",
       "      <td>0.079294</td>\n",
       "      <td>0.038617</td>\n",
       "      <td>0.027319</td>\n",
       "      <td>...</td>\n",
       "      <td>4.82232</td>\n",
       "      <td>6.141662</td>\n",
       "      <td>33.474687</td>\n",
       "      <td>567.846267</td>\n",
       "      <td>0.022818</td>\n",
       "      <td>0.156523</td>\n",
       "      <td>0.207989</td>\n",
       "      <td>0.065484</td>\n",
       "      <td>0.061508</td>\n",
       "      <td>0.018017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>8.670000e+03</td>\n",
       "      <td>6.981000</td>\n",
       "      <td>9.710000</td>\n",
       "      <td>43.790000</td>\n",
       "      <td>143.500000</td>\n",
       "      <td>0.052630</td>\n",
       "      <td>0.019380</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.106000</td>\n",
       "      <td>...</td>\n",
       "      <td>7.93000</td>\n",
       "      <td>12.020000</td>\n",
       "      <td>50.410000</td>\n",
       "      <td>185.200000</td>\n",
       "      <td>0.071170</td>\n",
       "      <td>0.027290</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.156500</td>\n",
       "      <td>0.055040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>8.692225e+05</td>\n",
       "      <td>11.697500</td>\n",
       "      <td>16.177500</td>\n",
       "      <td>75.135000</td>\n",
       "      <td>420.175000</td>\n",
       "      <td>0.086290</td>\n",
       "      <td>0.064815</td>\n",
       "      <td>0.029540</td>\n",
       "      <td>0.020310</td>\n",
       "      <td>0.161900</td>\n",
       "      <td>...</td>\n",
       "      <td>13.01000</td>\n",
       "      <td>21.095000</td>\n",
       "      <td>84.102500</td>\n",
       "      <td>514.975000</td>\n",
       "      <td>0.116600</td>\n",
       "      <td>0.146900</td>\n",
       "      <td>0.114475</td>\n",
       "      <td>0.064730</td>\n",
       "      <td>0.250350</td>\n",
       "      <td>0.071412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>9.061570e+05</td>\n",
       "      <td>13.355000</td>\n",
       "      <td>18.855000</td>\n",
       "      <td>86.210000</td>\n",
       "      <td>548.750000</td>\n",
       "      <td>0.095865</td>\n",
       "      <td>0.092525</td>\n",
       "      <td>0.061400</td>\n",
       "      <td>0.033455</td>\n",
       "      <td>0.179200</td>\n",
       "      <td>...</td>\n",
       "      <td>14.96500</td>\n",
       "      <td>25.425000</td>\n",
       "      <td>97.655000</td>\n",
       "      <td>685.550000</td>\n",
       "      <td>0.131300</td>\n",
       "      <td>0.211850</td>\n",
       "      <td>0.226550</td>\n",
       "      <td>0.099840</td>\n",
       "      <td>0.282050</td>\n",
       "      <td>0.080015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>8.825022e+06</td>\n",
       "      <td>15.780000</td>\n",
       "      <td>21.802500</td>\n",
       "      <td>103.875000</td>\n",
       "      <td>782.625000</td>\n",
       "      <td>0.105300</td>\n",
       "      <td>0.130400</td>\n",
       "      <td>0.129650</td>\n",
       "      <td>0.073730</td>\n",
       "      <td>0.195625</td>\n",
       "      <td>...</td>\n",
       "      <td>18.76750</td>\n",
       "      <td>29.757500</td>\n",
       "      <td>125.175000</td>\n",
       "      <td>1073.500000</td>\n",
       "      <td>0.146000</td>\n",
       "      <td>0.337600</td>\n",
       "      <td>0.381400</td>\n",
       "      <td>0.161325</td>\n",
       "      <td>0.317675</td>\n",
       "      <td>0.092065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>9.113205e+08</td>\n",
       "      <td>28.110000</td>\n",
       "      <td>39.280000</td>\n",
       "      <td>188.500000</td>\n",
       "      <td>2501.000000</td>\n",
       "      <td>0.163400</td>\n",
       "      <td>0.345400</td>\n",
       "      <td>0.426800</td>\n",
       "      <td>0.201200</td>\n",
       "      <td>0.304000</td>\n",
       "      <td>...</td>\n",
       "      <td>36.04000</td>\n",
       "      <td>49.540000</td>\n",
       "      <td>251.200000</td>\n",
       "      <td>4254.000000</td>\n",
       "      <td>0.222600</td>\n",
       "      <td>1.058000</td>\n",
       "      <td>1.252000</td>\n",
       "      <td>0.291000</td>\n",
       "      <td>0.663800</td>\n",
       "      <td>0.207500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             842302       17.99       10.38       122.8         1001  \\\n",
       "count  5.680000e+02  568.000000  568.000000  568.000000   568.000000   \n",
       "mean   3.042382e+07   14.120491   19.305335   91.914754   654.279754   \n",
       "std    1.251246e+08    3.523416    4.288506   24.285848   351.923751   \n",
       "min    8.670000e+03    6.981000    9.710000   43.790000   143.500000   \n",
       "25%    8.692225e+05   11.697500   16.177500   75.135000   420.175000   \n",
       "50%    9.061570e+05   13.355000   18.855000   86.210000   548.750000   \n",
       "75%    8.825022e+06   15.780000   21.802500  103.875000   782.625000   \n",
       "max    9.113205e+08   28.110000   39.280000  188.500000  2501.000000   \n",
       "\n",
       "           0.1184      0.2776      0.3001      0.1471      0.2419  ...  \\\n",
       "count  568.000000  568.000000  568.000000  568.000000  568.000000  ...   \n",
       "mean     0.096321    0.104036    0.088427    0.048746    0.181055  ...   \n",
       "std      0.014046    0.052355    0.079294    0.038617    0.027319  ...   \n",
       "min      0.052630    0.019380    0.000000    0.000000    0.106000  ...   \n",
       "25%      0.086290    0.064815    0.029540    0.020310    0.161900  ...   \n",
       "50%      0.095865    0.092525    0.061400    0.033455    0.179200  ...   \n",
       "75%      0.105300    0.130400    0.129650    0.073730    0.195625  ...   \n",
       "max      0.163400    0.345400    0.426800    0.201200    0.304000  ...   \n",
       "\n",
       "           25.38       17.33       184.6         2019      0.1622      0.6656  \\\n",
       "count  568.00000  568.000000  568.000000   568.000000  568.000000  568.000000   \n",
       "mean    16.25315   25.691919  107.125053   878.578873    0.132316    0.253541   \n",
       "std      4.82232    6.141662   33.474687   567.846267    0.022818    0.156523   \n",
       "min      7.93000   12.020000   50.410000   185.200000    0.071170    0.027290   \n",
       "25%     13.01000   21.095000   84.102500   514.975000    0.116600    0.146900   \n",
       "50%     14.96500   25.425000   97.655000   685.550000    0.131300    0.211850   \n",
       "75%     18.76750   29.757500  125.175000  1073.500000    0.146000    0.337600   \n",
       "max     36.04000   49.540000  251.200000  4254.000000    0.222600    1.058000   \n",
       "\n",
       "           0.7119      0.2654      0.4601      0.1189  \n",
       "count  568.000000  568.000000  568.000000  568.000000  \n",
       "mean     0.271414    0.114341    0.289776    0.083884  \n",
       "std      0.207989    0.065484    0.061508    0.018017  \n",
       "min      0.000000    0.000000    0.156500    0.055040  \n",
       "25%      0.114475    0.064730    0.250350    0.071412  \n",
       "50%      0.226550    0.099840    0.282050    0.080015  \n",
       "75%      0.381400    0.161325    0.317675    0.092065  \n",
       "max      1.252000    0.291000    0.663800    0.207500  \n",
       "\n",
       "[8 rows x 31 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "credit_df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "212db8f1-ef56-408c-92d2-faf5f584c8a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "842302      0\n",
       "M           0\n",
       "17.99       0\n",
       "10.38       0\n",
       "122.8       0\n",
       "1001        0\n",
       "0.1184      0\n",
       "0.2776      0\n",
       "0.3001      0\n",
       "0.1471      0\n",
       "0.2419      0\n",
       "0.07871     0\n",
       "1.095       0\n",
       "0.9053      0\n",
       "8.589       0\n",
       "153.4       0\n",
       "0.006399    0\n",
       "0.04904     0\n",
       "0.05373     0\n",
       "0.01587     0\n",
       "0.03003     0\n",
       "0.006193    0\n",
       "25.38       0\n",
       "17.33       0\n",
       "184.6       0\n",
       "2019        0\n",
       "0.1622      0\n",
       "0.6656      0\n",
       "0.7119      0\n",
       "0.2654      0\n",
       "0.4601      0\n",
       "0.1189      0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "credit_df.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7704534b-54fd-438d-8e67-04cdc4806813",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>842302</th>\n",
       "      <th>M</th>\n",
       "      <th>17.99</th>\n",
       "      <th>10.38</th>\n",
       "      <th>122.8</th>\n",
       "      <th>1001</th>\n",
       "      <th>0.1184</th>\n",
       "      <th>0.2776</th>\n",
       "      <th>0.3001</th>\n",
       "      <th>0.1471</th>\n",
       "      <th>...</th>\n",
       "      <th>25.38</th>\n",
       "      <th>17.33</th>\n",
       "      <th>184.6</th>\n",
       "      <th>2019</th>\n",
       "      <th>0.1622</th>\n",
       "      <th>0.6656</th>\n",
       "      <th>0.7119</th>\n",
       "      <th>0.2654</th>\n",
       "      <th>0.4601</th>\n",
       "      <th>0.1189</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>0 rows × 32 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [842302, M, 17.99, 10.38, 122.8, 1001, 0.1184, 0.2776, 0.3001, 0.1471, 0.2419, 0.07871, 1.095, 0.9053, 8.589, 153.4, 0.006399, 0.04904, 0.05373, 0.01587, 0.03003, 0.006193, 25.38, 17.33, 184.6, 2019, 0.1622, 0.6656, 0.7119, 0.2654, 0.4601, 0.1189]\n",
       "Index: []\n",
       "\n",
       "[0 rows x 32 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "credit_df[credit_df.duplicated()]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdde8eea-0d4e-4dc3-816b-d94a25b03201",
   "metadata": {},
   "source": [
    "## 训练集&测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ad4271fb-68ab-4008-bcc4-d803e7c5fca9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(568, 33)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "credit_df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "60fa083d-f1c2-4044-b678-8ccb7023c410",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        17.99     10.38     122.8      1001    0.1184    0.2776    0.3001  \\\n",
      "0    0.643144  0.272574  0.615783  0.501591  0.289880  0.181768  0.203608   \n",
      "1    0.601496  0.390260  0.595743  0.449417  0.514309  0.431017  0.462512   \n",
      "2    0.210090  0.360839  0.233501  0.102906  0.811321  0.811361  0.565604   \n",
      "3    0.629893  0.156578  0.630986  0.489290  0.430351  0.347893  0.463918   \n",
      "4    0.258839  0.202570  0.267984  0.141506  0.678613  0.461996  0.369728   \n",
      "..        ...       ...       ...       ...       ...       ...       ...   \n",
      "563  0.690000  0.428813  0.678668  0.566490  0.526948  0.296055  0.571462   \n",
      "564  0.622320  0.626987  0.604036  0.474019  0.407782  0.257714  0.337395   \n",
      "565  0.455251  0.621238  0.445788  0.303118  0.288165  0.254340  0.216753   \n",
      "566  0.644564  0.663510  0.665538  0.475716  0.588336  0.790197  0.823336   \n",
      "567  0.036869  0.501522  0.028540  0.015907  0.000000  0.074351  0.000000   \n",
      "\n",
      "       0.1471    0.2419   0.07871  ...     25.38     17.33     184.6  \\\n",
      "0    0.348757  0.379798  0.141323  ...  0.606901  0.303571  0.539818   \n",
      "1    0.635686  0.509596  0.211247  ...  0.556386  0.360075  0.508442   \n",
      "2    0.522863  0.776263  1.000000  ...  0.248310  0.385928  0.241347   \n",
      "3    0.518390  0.378283  0.186816  ...  0.519744  0.123934  0.506948   \n",
      "4    0.402038  0.518687  0.551179  ...  0.268232  0.312633  0.263908   \n",
      "..        ...       ...       ...  ...       ...       ...       ...   \n",
      "563  0.690358  0.336364  0.132056  ...  0.623266  0.383262  0.576174   \n",
      "564  0.486630  0.349495  0.113100  ...  0.560655  0.699094  0.520892   \n",
      "565  0.263519  0.267677  0.137321  ...  0.393099  0.589019  0.379949   \n",
      "566  0.755467  0.675253  0.425442  ...  0.633582  0.730277  0.668310   \n",
      "567  0.000000  0.266162  0.187026  ...  0.054287  0.489072  0.043578   \n",
      "\n",
      "         2019    0.1622    0.6656    0.7119    0.2654    0.4601    0.1189  \n",
      "0    0.435214  0.347553  0.154563  0.192971  0.639175  0.233590  0.222878  \n",
      "1    0.374508  0.483590  0.385375  0.359744  0.835052  0.403706  0.213433  \n",
      "2    0.094008  0.915472  0.814012  0.548642  0.884880  1.000000  0.773711  \n",
      "3    0.341575  0.437364  0.172415  0.319489  0.558419  0.157500  0.142595  \n",
      "4    0.136748  0.712739  0.482784  0.427716  0.598282  0.477035  0.454939  \n",
      "..        ...       ...       ...       ...       ...       ...       ...  \n",
      "563  0.452664  0.461137  0.178527  0.328035  0.761512  0.097575  0.105667  \n",
      "564  0.379915  0.300007  0.159997  0.256789  0.559450  0.198502  0.074315  \n",
      "565  0.230731  0.282177  0.273705  0.271805  0.487285  0.128721  0.151909  \n",
      "566  0.402035  0.619626  0.815758  0.749760  0.910653  0.497142  0.452315  \n",
      "567  0.020497  0.124084  0.036043  0.000000  0.000000  0.257441  0.100682  \n",
      "\n",
      "[568 rows x 30 columns]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "target\n",
       "0    357\n",
       "1    211\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "credit_df['target'] = 0\n",
    "credit_df.loc[(credit_df.iloc[:, 1] == 'M'), 'target'] = 1\n",
    "credit_df.loc[(credit_df.iloc[:, 1] == 'B'), 'target'] = 0\n",
    "X = credit_df.iloc[:,2:32]\n",
    "X_normalized = (X - X.min()) / (X.max() - X.min())\n",
    "print(X_normalized)\n",
    "\n",
    "trainData_y = credit_df['target'] \n",
    "trainData_y.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "48d72134-ebd0-4983-83ee-ac191d0c735b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       842302  M  17.99  10.38   122.8    1001   0.1184   0.2776   0.3001  \\\n",
      "0      842517  M  20.57  17.77  132.90  1326.0  0.08474  0.07864  0.08690   \n",
      "1    84300903  M  19.69  21.25  130.00  1203.0  0.10960  0.15990  0.19740   \n",
      "2    84348301  M  11.42  20.38   77.58   386.1  0.14250  0.28390  0.24140   \n",
      "3    84358402  M  20.29  14.34  135.10  1297.0  0.10030  0.13280  0.19800   \n",
      "4      843786  M  12.45  15.70   82.57   477.1  0.12780  0.17000  0.15780   \n",
      "..        ... ..    ...    ...     ...     ...      ...      ...      ...   \n",
      "563    926424  M  21.56  22.39  142.00  1479.0  0.11100  0.11590  0.24390   \n",
      "564    926682  M  20.13  28.25  131.20  1261.0  0.09780  0.10340  0.14400   \n",
      "565    926954  M  16.60  28.08  108.30   858.1  0.08455  0.10230  0.09251   \n",
      "566    927241  M  20.60  29.33  140.10  1265.0  0.11780  0.27700  0.35140   \n",
      "567     92751  B   7.76  24.54   47.92   181.0  0.05263  0.04362  0.00000   \n",
      "\n",
      "      0.1471  ...  17.33   184.6    2019   0.1622   0.6656  0.7119  0.2654  \\\n",
      "0    0.07017  ...  23.41  158.80  1956.0  0.12380  0.18660  0.2416  0.1860   \n",
      "1    0.12790  ...  25.53  152.50  1709.0  0.14440  0.42450  0.4504  0.2430   \n",
      "2    0.10520  ...  26.50   98.87   567.7  0.20980  0.86630  0.6869  0.2575   \n",
      "3    0.10430  ...  16.67  152.20  1575.0  0.13740  0.20500  0.4000  0.1625   \n",
      "4    0.08089  ...  23.75  103.40   741.6  0.17910  0.52490  0.5355  0.1741   \n",
      "..       ...  ...    ...     ...     ...      ...      ...     ...     ...   \n",
      "563  0.13890  ...  26.40  166.10  2027.0  0.14100  0.21130  0.4107  0.2216   \n",
      "564  0.09791  ...  38.25  155.00  1731.0  0.11660  0.19220  0.3215  0.1628   \n",
      "565  0.05302  ...  34.12  126.70  1124.0  0.11390  0.30940  0.3403  0.1418   \n",
      "566  0.15200  ...  39.42  184.60  1821.0  0.16500  0.86810  0.9387  0.2650   \n",
      "567  0.00000  ...  30.37   59.16   268.6  0.08996  0.06444  0.0000  0.0000   \n",
      "\n",
      "     0.4601   0.1189  target  \n",
      "0    0.2750  0.08902       1  \n",
      "1    0.3613  0.08758       1  \n",
      "2    0.6638  0.17300       1  \n",
      "3    0.2364  0.07678       1  \n",
      "4    0.3985  0.12440       1  \n",
      "..      ...      ...     ...  \n",
      "563  0.2060  0.07115       1  \n",
      "564  0.2572  0.06637       1  \n",
      "565  0.2218  0.07820       1  \n",
      "566  0.4087  0.12400       1  \n",
      "567  0.2871  0.07039       0  \n",
      "\n",
      "[568 rows x 33 columns]\n"
     ]
    }
   ],
   "source": [
    "print(credit_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "f0b8505a-7acd-472a-8fca-53e162822bd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "trainData_X = credit_df.iloc[:, [0] + list(range(2, credit_df.shape[1]))]\n",
    "trainData_y = credit_df['target']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "15752457-ed00-4b78-97a9-8a237118c20a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-2 {\n",
       "  /* Definition of color scheme common for light and dark mode */\n",
       "  --sklearn-color-text: black;\n",
       "  --sklearn-color-line: gray;\n",
       "  /* Definition of color scheme for unfitted estimators */\n",
       "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
       "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
       "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
       "  --sklearn-color-unfitted-level-3: chocolate;\n",
       "  /* Definition of color scheme for fitted estimators */\n",
       "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
       "  --sklearn-color-fitted-level-1: #d4ebff;\n",
       "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
       "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
       "\n",
       "  /* Specific color for light theme */\n",
       "  --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",
       "  --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-icon: #696969;\n",
       "\n",
       "  @media (prefers-color-scheme: dark) {\n",
       "    /* Redefinition of color scheme for dark theme */\n",
       "    --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",
       "    --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-icon: #878787;\n",
       "  }\n",
       "}\n",
       "\n",
       "#sk-container-id-2 {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 pre {\n",
       "  padding: 0;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 input.sk-hidden--visually {\n",
       "  border: 0;\n",
       "  clip: rect(1px 1px 1px 1px);\n",
       "  clip: rect(1px, 1px, 1px, 1px);\n",
       "  height: 1px;\n",
       "  margin: -1px;\n",
       "  overflow: hidden;\n",
       "  padding: 0;\n",
       "  position: absolute;\n",
       "  width: 1px;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-dashed-wrapped {\n",
       "  border: 1px dashed var(--sklearn-color-line);\n",
       "  margin: 0 0.4em 0.5em 0.4em;\n",
       "  box-sizing: border-box;\n",
       "  padding-bottom: 0.4em;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-container {\n",
       "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
       "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
       "     so we also need the `!important` here to be able to override the\n",
       "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
       "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
       "  display: inline-block !important;\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-text-repr-fallback {\n",
       "  display: none;\n",
       "}\n",
       "\n",
       "div.sk-parallel-item,\n",
       "div.sk-serial,\n",
       "div.sk-item {\n",
       "  /* draw centered vertical line to link estimators */\n",
       "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
       "  background-size: 2px 100%;\n",
       "  background-repeat: no-repeat;\n",
       "  background-position: center center;\n",
       "}\n",
       "\n",
       "/* Parallel-specific style estimator block */\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item::after {\n",
       "  content: \"\";\n",
       "  width: 100%;\n",
       "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
       "  flex-grow: 1;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel {\n",
       "  display: flex;\n",
       "  align-items: stretch;\n",
       "  justify-content: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item:first-child::after {\n",
       "  align-self: flex-end;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item:last-child::after {\n",
       "  align-self: flex-start;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item:only-child::after {\n",
       "  width: 0;\n",
       "}\n",
       "\n",
       "/* Serial-specific style estimator block */\n",
       "\n",
       "#sk-container-id-2 div.sk-serial {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  align-items: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  padding-right: 1em;\n",
       "  padding-left: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
       "clickable and can be expanded/collapsed.\n",
       "- Pipeline and ColumnTransformer use this feature and define the default style\n",
       "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
       "*/\n",
       "\n",
       "/* Pipeline and ColumnTransformer style (default) */\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable {\n",
       "  /* Default theme specific background. It is overwritten whether we have a\n",
       "  specific estimator or a Pipeline/ColumnTransformer */\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "/* Toggleable label */\n",
       "#sk-container-id-2 label.sk-toggleable__label {\n",
       "  cursor: pointer;\n",
       "  display: block;\n",
       "  width: 100%;\n",
       "  margin-bottom: 0;\n",
       "  padding: 0.5em;\n",
       "  box-sizing: border-box;\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 label.sk-toggleable__label-arrow:before {\n",
       "  /* Arrow on the left of the label */\n",
       "  content: \"▸\";\n",
       "  float: left;\n",
       "  margin-right: 0.25em;\n",
       "  color: var(--sklearn-color-icon);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "/* Toggleable content - dropdown */\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content {\n",
       "  max-height: 0;\n",
       "  max-width: 0;\n",
       "  overflow: hidden;\n",
       "  text-align: left;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content pre {\n",
       "  margin: 0.2em;\n",
       "  border-radius: 0.25em;\n",
       "  color: var(--sklearn-color-text);\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content.fitted pre {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
       "  /* Expand drop-down */\n",
       "  max-height: 200px;\n",
       "  max-width: 100%;\n",
       "  overflow: auto;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
       "  content: \"▾\";\n",
       "}\n",
       "\n",
       "/* Pipeline/ColumnTransformer-specific style */\n",
       "\n",
       "#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator-specific style */\n",
       "\n",
       "/* Colorize estimator box */\n",
       "#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-label label.sk-toggleable__label,\n",
       "#sk-container-id-2 div.sk-label label {\n",
       "  /* The background is the default theme color */\n",
       "  color: var(--sklearn-color-text-on-default-background);\n",
       "}\n",
       "\n",
       "/* On hover, darken the color of the background */\n",
       "#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "/* Label box, darken color on hover, fitted */\n",
       "#sk-container-id-2 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator label */\n",
       "\n",
       "#sk-container-id-2 div.sk-label label {\n",
       "  font-family: monospace;\n",
       "  font-weight: bold;\n",
       "  display: inline-block;\n",
       "  line-height: 1.2em;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-label-container {\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "/* Estimator-specific */\n",
       "#sk-container-id-2 div.sk-estimator {\n",
       "  font-family: monospace;\n",
       "  border: 1px dotted var(--sklearn-color-border-box);\n",
       "  border-radius: 0.25em;\n",
       "  box-sizing: border-box;\n",
       "  margin-bottom: 0.5em;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-estimator.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "/* on hover */\n",
       "#sk-container-id-2 div.sk-estimator:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-estimator.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
       "\n",
       "/* Common style for \"i\" and \"?\" */\n",
       "\n",
       ".sk-estimator-doc-link,\n",
       "a:link.sk-estimator-doc-link,\n",
       "a:visited.sk-estimator-doc-link {\n",
       "  float: right;\n",
       "  font-size: smaller;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1em;\n",
       "  height: 1em;\n",
       "  width: 1em;\n",
       "  text-decoration: none !important;\n",
       "  margin-left: 1ex;\n",
       "  /* unfitted */\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted,\n",
       "a:link.sk-estimator-doc-link.fitted,\n",
       "a:visited.sk-estimator-doc-link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "/* Span, style for the box shown on hovering the info icon */\n",
       ".sk-estimator-doc-link span {\n",
       "  display: none;\n",
       "  z-index: 9999;\n",
       "  position: relative;\n",
       "  font-weight: normal;\n",
       "  right: .2ex;\n",
       "  padding: .5ex;\n",
       "  margin: .5ex;\n",
       "  width: min-content;\n",
       "  min-width: 20ex;\n",
       "  max-width: 50ex;\n",
       "  color: var(--sklearn-color-text);\n",
       "  box-shadow: 2pt 2pt 4pt #999;\n",
       "  /* unfitted */\n",
       "  background: var(--sklearn-color-unfitted-level-0);\n",
       "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted span {\n",
       "  /* fitted */\n",
       "  background: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link:hover span {\n",
       "  display: block;\n",
       "}\n",
       "\n",
       "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
       "\n",
       "#sk-container-id-2 a.estimator_doc_link {\n",
       "  float: right;\n",
       "  font-size: 1rem;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1rem;\n",
       "  height: 1rem;\n",
       "  width: 1rem;\n",
       "  text-decoration: none;\n",
       "  /* unfitted */\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 a.estimator_doc_link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "#sk-container-id-2 a.estimator_doc_link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 a.estimator_doc_link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LogisticRegression(solver=&#x27;liblinear&#x27;)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;&nbsp;LogisticRegression<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.linear_model.LogisticRegression.html\">?<span>Documentation for LogisticRegression</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></label><div class=\"sk-toggleable__content fitted\"><pre>LogisticRegression(solver=&#x27;liblinear&#x27;)</pre></div> </div></div></div></div>"
      ],
      "text/plain": [
       "LogisticRegression(solver='liblinear')"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X_normalized, trainData_y, test_size=0.3, random_state=42)\n",
    "\n",
    "lr = LogisticRegression(solver='liblinear')\n",
    "lr.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "d6a7b19f-e040-44c7-9626-491e8c16c3a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-5.17742604]\n",
      "[[ 0.86757664  0.88818925  0.93137793  1.11524614 -0.32908442  0.48180738\n",
      "   1.6310877   2.20701242 -0.25653572 -1.66946043  1.00815716 -0.51182778\n",
      "   0.79784615  0.7738937  -0.21319014 -0.47882335 -0.29082885 -0.19412902\n",
      "  -0.75642245 -0.64623784  1.54487015  1.45842361  1.48050839  1.38659871\n",
      "   0.73209798  0.79964286  1.29342512  2.1322337   0.63102856 -0.00349023]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(1, 30)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(lr.intercept_ )\n",
    "print(lr.coef_)\n",
    "lr.coef_.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "824cbdf2-27c2-4e19-b434-27e41728f1db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LogisticRegression(C=10, max_iter=1000, penalty='l1', solver='liblinear')\n",
      "0.9798101265822785\n",
      "{'C': 10, 'penalty': 'l1'}\n"
     ]
    }
   ],
   "source": [
    "# grid search调参\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "parameters = {'penalty': ('l1', 'l2'),'C': (0.01, 0.1, 0.8, 0.9, 0.95, 1, 1.1, 2, 5, 10),}\n",
    "\n",
    "lr = LogisticRegression(solver='liblinear', max_iter=1000)   #增加迭代次数避免发散\n",
    "lr_search = GridSearchCV(lr, parameters, scoring='accuracy', cv=5)   #用GridSearchCV函数依次在模型中代入希望的参数调节，用准确率打分\n",
    "lr_search.fit(X_train, y_train)\n",
    "\n",
    "#查看最佳结果\n",
    "print(lr_search.best_estimator_)\n",
    "print(lr_search.best_score_)\n",
    "print(lr_search.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ae96c79d-257e-4e07-a139-c995278919ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.94      1.00      0.97       103\n",
      "           1       1.00      0.91      0.95        68\n",
      "\n",
      "    accuracy                           0.96       171\n",
      "   macro avg       0.97      0.96      0.96       171\n",
      "weighted avg       0.97      0.96      0.96       171\n",
      "\n",
      "[[103   0]\n",
      " [  6  62]]\n"
     ]
    }
   ],
   "source": [
    "y_test_pred = lr.predict(X_test)\n",
    "y_test_prob = lr.predict_proba(X_test)\n",
    "print(classification_report(y_test,y_test_pred))\n",
    "print(confusion_matrix(y_test,y_test_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d17b479b-2b0f-4676-b77d-6d43fddbd3e6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
